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

本秂侑毒 提交于 2019-12-07 13:35:40

问题


I'm trying to use the MySQLRoleProvider(MySql.Web, Version=6.2.2.0) with Visual Web Developer 2008.

When trying to add a role I get an exception "Table 'test.my_aspnet_applications' doesn't exist"

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

Can someone tell me where I went wrong. Or tell me how to generate / find the correct database script to create the role, membership, profile ... MySql tables.

    <membership defaultProvider="MySQLMembershipProvider">
        <providers>
            <remove name="MySQLMembershipProvider"/>
            <add autogenerateschema="true" connectionStringName="LocalMySqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
        </providers>
    </membership>
    <profile enabled="true" defaultProvider="MySQLProfileProvider">
        <providers>
            <remove name="MySQLProfileProvider"/>
            <add name="MySQLProfileProvider" autogenerateschema="true" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/"/>
        </providers>
    </profile>
    <roleManager enabled="true" defaultProvider="MySQLRoleProvider">
        <providers>
            <remove name="MySQLRoleProvider"/>
            <add autogenerateschema="true" connectionStringName="LocalMySqlServer" applicationName="/" name="MySQLRoleProvider" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
        </providers>
    </roleManager>      

回答1:


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.




回答2:


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();
    }
}



回答3:


Follow this codeproject howto and you'll be fine.



来源:https://stackoverflow.com/questions/2242644/using-mysql-mysqlmembershipprovider-autogenerateschema-true-not-working

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