使用MySql MySQLMembershipProvider – autogenerateschema =“true”不起作用?

我正在尝试在Visual Web Developer 2008中使用MySQLRoleProvider(MySql.Web,Version = 6.2.2.0)。

在尝试添加角色时,我得到一个例外“表’test.my_aspnet_applications’不存在”

if (!Roles.RoleExists("TestRole")) { Roles.CreateRole("TestRole"); } 

有人能告诉我哪里出错了。 或者告诉我如何生成/找到正确的数据库脚本来创建角色,成员资格,配置文件…… MySql表。

                    

您是否使用ASP.Net配置工具将应用程序的提供程序切换到MySQL提供程序? 我相信这是触发MySQL提供程序自动生成模式的原因。

如果你的数据库不会生成试试这个:

创建一个自定义ContextInitializer并将其添加到Global.asax:

  Database.SetInitializer(new CreateMySqlDatabaseIfNotExists()); internal class CreateMySqlDatabaseIfNotExists: IDatabaseInitializer where TContext : MyContext { public void InitializeDatabase(TContext context) { if (context.Database.Exists()) { if (!context.Database.CompatibleWithModel(false)) throw new InvalidOperationException("The model has changed!"); } else { CreateMySqlDatabase(context); Seed(context); } } private void CreateMySqlDatabase(TContext context) { try { context.Database.Create(); return; } catch (MySqlException ex) { // Ignore the parse exception if (ex.Number != 1064) { throw; } } // Manually create the metadata table using (var connection = ((MySqlConnection) context .Database.Connection).Clone()) using (var command = connection.CreateCommand()) { command.CommandText = @" CREATE TABLE __MigrationHistory ( MigrationId mediumtext NOT NULL, CreatedOn datetime NOT NULL, Model mediumblob NOT NULL, ProductVersion mediumtext NOT NULL); ALTER TABLE __MigrationHistory ADD PRIMARY KEY (MigrationId(255)); INSERT INTO __MigrationHistory ( MigrationId, CreatedOn, Model, ProductVersion) VALUES ( 'InitialCreate', @CreatedOn, @Model, @ProductVersion); "; command.Parameters.AddWithValue( "@Model", GetModel(context)); command.Parameters.AddWithValue( "@ProductVersion", GetProductVersion()); command.Parameters.AddWithValue( "@CreatedOn", DateTime.Now); connection.Open(); command.ExecuteNonQuery(); } } private byte[] GetModel(TContext context) { using (var memoryStream = new MemoryStream()) { using (var gzipStream = new GZipStream( memoryStream, CompressionMode.Compress)) using (var xmlWriter = XmlWriter.Create( gzipStream, new XmlWriterSettings {Indent = true})) { EdmxWriter.WriteEdmx(context, xmlWriter); } return memoryStream.ToArray(); } } private string GetProductVersion() { return typeof (DbContext).Assembly .GetCustomAttributes(false) .OfType() .Single() .InformationalVersion; } protected void Seed(TContext context) { // ... context.SaveChanges(); } } 

按照这个代码项目如何 ,你会没事的。