【发布时间】:2015-09-25 05:56:03
【问题描述】:
我创建了一个自定义数据库初始化程序,以便在每次使用 DropCreateDatabaseIfModelChanges 初始化程序创建数据库时覆盖种子方法并将数据添加到数据库。见以下代码:
public class BehaviourContext : DbContext
{
public BehaviourContext(): base("name=BehaviourDBConnectionString")
{
Database.SetInitializer<BehaviourContext>(new BehaviourInitializer<BehaviourContext>());
}
public DbSet<Behaviour> Behaviours { get; set; }
public DbSet<Precondition> Preconditions { get; set; }
public DbSet<AddList> AddLists { get; set; }
public DbSet<DeleteList> DeleteLists { get; set; }
public DbSet<AtomicBehaviour> AtomicBehaviours { get; set; }
}
public class BehaviourInitializer<T> : DropCreateDatabaseIfModelChanges<BehaviourContext>
{
protected override void Seed(BehaviourContext context)
{
//Add Seed data for the database
IList<Behaviour> defaultBehaviours = new List<Behaviour>();
defaultBehaviours.Add(new Behaviour()
{
Activation_Threshold = 90,
Currently_Executing = false,
Name = "Test Behaviour",
Preconditions_Met = false,
Priority = 0.9f,
Preconditions = new List<Precondition>() { new Precondition() { Precondition_Name = "test precondition 1", Value = "test value 1" }, new Precondition() { Precondition_Name = "test precondition 2", Value = "test value 2" } },
AddLists = new List<AddList>() { new AddList() { Name = "test add list 1" }, new AddList() { Name = "test add list 2"} },
DeleteList = new List<DeleteList>() { new DeleteList() { Name = "test delete list 1" }, new DeleteList() { Name = "test delete list 2"} },
AtomicList = new List<AtomicBehaviour>() { new AtomicBehaviour() { Name = "test atomic behaviour 1" }, new AtomicBehaviour(){Name = "test atomic behaviour 2"}}
});
base.Seed(context);
}
}
所以我的问题是如何使用自定义初始化程序启用自动迁移?下面的代码显示了我这样做的尝试:
public class BehaviourContext : DbContext
{
public BehaviourContext(): base("name=BehaviourDBConnectionString")
{
Database.SetInitializer<BehaviourContext>(new BehaviourInitializer<BehaviourContext>());
//Database.SetInitializer<BehaviourContext>(new MigrateDatabaseToLatestVersion<BehaviourContext, System_Architecture.Migrations.Configuration>("name=BehaviourDBConnectionString"));
}
public DbSet<Behaviour> Behaviours { get; set; }
public DbSet<Precondition> Preconditions { get; set; }
public DbSet<AddList> AddLists { get; set; }
public DbSet<DeleteList> DeleteLists { get; set; }
public DbSet<AtomicBehaviour> AtomicBehaviours { get; set; }
}
public class BehaviourInitializer<T> : MigrateDatabaseToLatestVersion<BehaviourContext,System_Architecture.Migrations.Configuration>
{
protected override void Seed(BehaviourContext context)
{
//Add Seed data for the database
IList<Behaviour> defaultBehaviours = new List<Behaviour>();
defaultBehaviours.Add(new Behaviour()
{
Activation_Threshold = 90,
Currently_Executing = false,
Name = "Test Behaviour",
Preconditions_Met = false,
Priority = 0.9f,
Preconditions = new List<Precondition>() { new Precondition() { Precondition_Name = "test precondition 1", Value = "test value 1" }, new Precondition() { Precondition_Name = "test precondition 2", Value = "test value 2" } },
AddLists = new List<AddList>() { new AddList() { Name = "test add list 1" }, new AddList() { Name = "test add list 2"} },
DeleteList = new List<DeleteList>() { new DeleteList() { Name = "test delete list 1" }, new DeleteList() { Name = "test delete list 2"} },
AtomicList = new List<AtomicBehaviour>() { new AtomicBehaviour() { Name = "test atomic behaviour 1" }, new AtomicBehaviour(){Name = "test atomic behaviour 2"}}
});
base.Seed(context);
}
}
BehaviourConext 类中注释掉的代码行是如何正常启用自动迁移,但是我不知道如何使用我的自定义初始化程序执行此操作,现在我收到当我尝试从 MigrateDatabaseToLatestVersion 继承时显示的错误。
有人有什么想法吗?
注意:我在包管理器中启用了自动迁移。
【问题讨论】:
标签: c# entity-framework database-migration automatic-migration