我已经学习了一个星期左右的时间来使用EF,并且一直困扰于创建/更新数据库的问题。如果数据库不存在,我可以创建一个初始化器来创建数据库:
static class Program { static void Main() { Database.SetInitializer<GumpDatabase>(new GumpDatabaseInitializer()); .... class GumpDatabaseInitializer : CreateDatabaseIfNotExists<GumpDatabase> { public GumpDatabaseInitializer() { } protected override void Seed(GumpDatabase context) { context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX Name ON Stations (Name)"); // Other stuff } }
或者我可以创建一个配置来迁移数据库
static class Program { static void Main() { Database.SetInitializer<GumpDatabase>(new MigrateDatabaseToLatestVersion<GumpDatabase, Configuration>()); .... internal sealed class Configuration : DbMigrationsConfiguration<GumpDatabase> { public Configuration() { AutomaticMigrationsEnabled = true; SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator()); } protected override void Seed(GumpDatabase context) { }
每个都可以正常工作,但我还没有想办法做到这两个。我可以通过更改SetInitializer调用在两个初始值设定项之间进行切换,但是如果我想创建数据库(如果该数据库不存在的话),并且还要迁移它(如果这样做的话)呢?我需要创建自定义初始化程序吗?
谢谢
根据NSGaga答案进行编辑
class CreateOrMigrateDatabaseInitializer<TContext, TConfiguration> : CreateDatabaseIfNotExists<TContext>, IDatabaseInitializer<TContext> where TContext : DbContext where TConfiguration : DbMigrationsConfiguration<TContext>, new() { private readonly DbMigrationsConfiguration _configuration; public CreateOrMigrateDatabaseInitializer() { _configuration = new TConfiguration(); } public CreateOrMigrateDatabaseInitializer(string connection) { Contract.Requires(!string.IsNullOrEmpty(connection), "connection"); _configuration = new TConfiguration { TargetDatabase = new DbConnectionInfo(connection) }; } void IDatabaseInitializer<TContext>.InitializeDatabase(TContext context) { Contract.Requires(context != null, "context"); if (context.Database.Exists()) { if (!context.Database.CompatibleWithModel(throwIfNoMetadata: false)) { var migrator = new DbMigrator(_configuration); migrator.Update(); } } else { context.Database.Create(); Seed(context); context.SaveChanges(); } } protected virtual void Seed(TContext context) { } }
和
internal sealed class Configuration : DbMigrationsConfiguration<GumpDatabase> { public Configuration() { AutomaticMigrationsEnabled = true; AutomaticMigrationDataLossAllowed = false; SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator()); } protected override void Seed(GumpDatabase context) { } }
class GumpDatabaseInitializer : CreateOrMigrateDatabaseInitializer<GumpDatabase,Gump.Migrations.Configuration> { public GumpDatabaseInitializer() { } protected override void Seed(GumpDatabase context) { context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX Name ON Stations (Name)"); context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX Name ON Sequences (Name)"); context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX StationPartNumber ON StationPartNumbers (StationId,PartNumberId)"); } }
最后
static void Main() { Database.SetInitializer<GumpDatabase>(new GumpDatabaseInitializer());
我认为您就在那儿- 您可以查找MigrateDatabaseToLatestVersion(它的开源http://entityframework.codeplex.com/)的源代码- 相当简单,它所做的几乎就是DbMigrator- 尽我所能看到。
MigrateDatabaseToLatestVersion
DbMigrator
您似乎要做的只是将两者合并-使用一个或另一个作为基础,在其中添加其他功能-我认为应该可以正常工作。
class CreateAndMigrateDatabaseInitializer<TContext, TConfiguration> : CreateDatabaseIfNotExists<TContext>, IDatabaseInitializer<TContext> where TContext : DbContext where TConfiguration : DbMigrationsConfiguration<TContext>, new() { private readonly DbMigrationsConfiguration _configuration; public CreateAndMigrateDatabaseInitializer() { _configuration = new TConfiguration(); } public CreateAndMigrateDatabaseInitializer(string connection) { Contract.Requires(!string.IsNullOrEmpty(connection), "connection"); _configuration = new TConfiguration { TargetDatabase = new DbConnectionInfo(connection) }; } void IDatabaseInitializer<TContext>.InitializeDatabase(TContext context) { Contract.Requires(context != null, "context"); var migrator = new DbMigrator(_configuration); migrator.Update(); // move on with the 'CreateDatabaseIfNotExists' for the 'Seed' base.InitializeDatabase(context); } protected override void Seed(TContext context) { } }
这样称呼…
Database.SetInitializer(new CreateAndMigrateDatabaseInitializer<GumpDatabase, YourNamespace.Migrations.Configuration>());
…实际上,像您所做的那样覆盖它(因为它是通用实现)CreateDatabaseIfNotExists(您只需要为Configuration添加额外的“ param”),然后提供“ Seed”即可。
CreateDatabaseIfNotExists
class GumpDatabaseInitializer : CreateAndMigrateDatabaseInitializer<GumpDatabase, YourNamespace.Migrations.Configuration> { protected override void Seed(GumpDatabase context) { context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX Name ON Stations (Name)"); } }
…并称其为
Database.SetInitializer(new GumpDatabaseInitializer());
编辑: 基于注释- DbMigrator不应运行两次。它总是检查(花费一些时间)并进行“空白”更新并继续。但是,以防万一,如果您想在输入之前删除并“检查”-这应该起作用(更改上面类似的部分)…
var migrator = new DbMigrator(_configuration); if (!context.Database.CompatibleWithModel(throwIfNoMetadata: false)) if (migrator.GetPendingMigrations().Any()) migrator.Update();
(这是多余的/仔细检查-if-s之一就足够了。在这里稍作休息- 看看Db迁移后到底发生了什么,它不应该进入。如我所提到的,当我将Db迁移后,它可以正常工作测试一下。
编辑:
InitializeDatabase用… 替换内部
InitializeDatabase
var doseed = !context.Database.Exists(); // && new DatabaseTableChecker().AnyModelTableExists(context); // check to see if to seed - we 'lack' the 'AnyModelTableExists' - could be copied/done otherwise if needed... var migrator = new DbMigrator(_configuration); // if (doseed || !context.Database.CompatibleWithModel(throwIfNoMetadata: false)) if (migrator.GetPendingMigrations().Any()) migrator.Update(); // move on with the 'CreateDatabaseIfNotExists' for the 'Seed' base.InitializeDatabase(context); if (doseed) { Seed(context); context.SaveChanges(); }
这可以解决(半路)不播种的情况-如果首先进行迁移。迁移必须是第一位的,否则您会遇到问题。
您仍然需要正确地进行操作-这不是您可能需要的所有内容的要点-但如果有MySQL等问题,这里可能还有更多工作要做。
注意: 如果您有数据库,则仍无法播种,但它为空。问题是两种不同的初始化器混合使用。因此,您必须解决此问题-通过实施Create …在内部执行的操作(该调用我们不能调用)或其他操作。