Using MySql MySQLMembershipProvider - autogenerateschema=“true” not working?

走远了吗. 提交于 2019-12-06 02:15:06

Have you used the ASP.Net configuration tool to switch your application's provider to the MySQL provider? I believe this is what triggers the MySQL provider to automatically generate the schema.

If your database won't generate try this:

Create a custom ContextInitializer and add this to the Global.asax:

 Database.SetInitializer(new CreateMySqlDatabaseIfNotExists<MyContext>());

 internal class CreateMySqlDatabaseIfNotExists<TContext>: IDatabaseInitializer<TContext> 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<AssemblyInformationalVersionAttribute>()
            .Single()
            .InformationalVersion;
    }

    protected void Seed(TContext context)
    { // ...
        context.SaveChanges();
    }
}

Follow this codeproject howto and you'll be fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!