的MySql与实体框架需要一些小小的调整。您需要创建三个类(您可以查看https://docs.microsoft.com/en-us/aspnet/identity/overview/getting-started/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider获取更多详细信息)。首先创建一个MySqlHistoryContext类。
public class MySqlHistoryContext : HistoryContext
{
public MySqlHistoryContext(
DbConnection existingConnection,
string defaultSchema)
: base(existingConnection, defaultSchema)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<HistoryRow>().Property(h =>
h.MigrationId).HasMaxLength(100).IsRequired();
modelBuilder.Entity<HistoryRow>().Property(h =>
h.ContextKey).HasMaxLength(200).IsRequired();
}
}
创建一个旁类MySqlConfiguration
public class MySqlConfiguration : DbConfiguration
{
public MySqlConfiguration()
{
SetHistoryContext(
"MySql.Data.MySqlClient", (conn, schema) => new MySqlHistoryContext(conn, schema));
}
}
创建未来
public class MySqlInitializer : IDatabaseInitializer<ApplicationDbContext>
{
public void InitializeDatabase(ApplicationDbContext context)
{
if (!context.Database.Exists())
{
// if database did not exist before - create it
context.Database.Create();
}
else
{
// query to check if MigrationHistory table is present in the database
var migrationHistoryTableExists =
((IObjectContextAdapter)context).ObjectContext.ExecuteStoreQuery<int>(
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema =
'IdentityMySQLDatabase' AND table_name = '__MigrationHistory'");
// if MigrationHistory table is not there (which is the case first time
we run) - create it
if (migrationHistoryTableExists.FirstOrDefault() == 0)
{
context.Database.Delete();
context.Database.Create();
}
}
}
}
打开IdentityModels.cs在MO MySqlInitializer类del文件夹。这增加了ApplicationDbContext:IdentityDbContext类
static ApplicationDbContext()
{
Database.SetInitializer(new MySqlInitializer());
}
那么,有没有“AlaysUpdate”选项?就像只知道如何添加表格和字段(非破坏性选项)的人一样? – BuddyJoe 2012-01-13 21:12:20
不幸的是,从EF 4.2开始,没有任何烘焙选项用于非破坏性更新。有一个NuGet包可用,称为EntityFramework.Migrations,可以帮助你(仍然在测试版),它应该包含在EF 4.3(目前也在测试版)中。您可以使用的另一个附加选项是RoundhousE - https://github.com/chucknorris/roundhouse,尽管我不确定它的维护情况。我个人推出了自己的迁移控制系统,尽管我期待着EF 4.3发布此功能。 – PinnyM 2012-01-15 03:31:43
很酷。谢谢。 +1 – BuddyJoe 2012-01-16 20:39:11