开发者

Using MySql MySQLMembershipProvider - autogenerateschema="true" not working?

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"开发者_StackOverflow 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>      


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.


Follow this codeproject howto and you'll be fine.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜