C# 如何在使用自定义DB初始值设定项c时启用自动迁移#
我已经创建了一个自定义数据库初始值设定项,以便每次使用DropCreateDatabaseIfModelChanges初始值设定项创建数据库时都可以重写seed方法并将数据添加到数据库中。见下面的代码:C# 如何在使用自定义DB初始值设定项c时启用自动迁移#,c#,entity-framework,database-migration,automatic-migration,C#,Entity Framework,Database Migration,Automatic Migration,我已经创建了一个自定义数据库初始值设定项,以便每次使用DropCreateDatabaseIfModelChanges初始值设定项创建数据库时都可以重写seed方法并将数据添加到数据库中。见下面的代码: public class BehaviourContext : DbContext { public BehaviourContext(): base("name=BehaviourDBConnectionString") { Database.SetInitia
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);
}
}
公共类行为上下文:DbContext
{
public behaviorContext():base(“name=behaviorDBConnectionString”)
{
Database.SetInitializer(新行为初始值设定项());
}
公共数据库集行为{get;set;}
公共DbSet预条件{get;set;}
公共DbSet AddLists{get;set;}
公共数据库集删除列表{get;set;}
公共DbSet原子行为{get;set;}
}
公共类行为初始值设定项:DropCreateDatabaseIfModelChanges
{
受保护的覆盖无效种子(行为上下文)
{
//为数据库添加种子数据
IList DefaultBehaviors=新列表();
DefaultBehaviors.Add(新行为()
{
激活\u阈值=90,
当前执行=false,
Name=“测试行为”,
前提条件_Met=false,
优先级=0.9f,
前提条件=新列表(){new premission(){premission_Name=“test premission 1”,Value=“test Value 1”},new premission(){premission_Name=“test premission 2”,Value=“test Value 2”},
AddList=new List(){new AddList(){Name=“test add List 1”},new AddList(){Name=“test add List 2”},
DeleteList=new List(){new DeleteList(){Name=“test delete List 1”},new DeleteList(){Name=“test delete List 2”},
AtomicList=new List(){new AtomicBehavior(){Name=“test AtomicBehavior 1”},new AtomicBehavior(){Name=“test AtomicBehavior 2”}
});
种子(上下文);
}
}
所以我的问题是如何使用自定义初始值设定项启用自动迁移?下面的代码显示了我的尝试:
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);
}
}
公共类行为上下文:DbContext
{
public behaviorContext():base(“name=behaviorDBConnectionString”)
{
Database.SetInitializer(新行为初始值设定项());
//SetInitializer(新的MigrateDatabaseToLatestVersion(“name=behaviousdbconnectionstring”);
}
公共数据库集行为{get;set;}
公共DbSet预条件{get;set;}
公共DbSet AddLists{get;set;}
公共数据库集删除列表{get;set;}
公共DbSet原子行为{get;set;}
}
公共类行为初始值设定项:MigrateDatabaseToLatestVersion
{
受保护的覆盖无效种子(行为上下文)
{
//为数据库添加种子数据
IList DefaultBehaviors=新列表();
DefaultBehaviors.Add(新行为()
{
激活\u阈值=90,
当前执行=false,
Name=“测试行为”,
前提条件_Met=false,
优先级=0.9f,
前提条件=新列表(){new premission(){premission_Name=“test premission 1”,Value=“test Value 1”},new premission(){premission_Name=“test premission 2”,Value=“test Value 2”},
AddList=new List(){new AddList(){Name=“test add List 1”},new AddList(){Name=“test add List 2”},
DeleteList=new List(){new DeleteList(){Name=“test delete List 1”},new DeleteList(){Name=“test delete List 2”},
AtomicList=new List(){new AtomicBehavior(){Name=“test AtomicBehavior 1”},new AtomicBehavior(){Name=“test AtomicBehavior 2”}
});
种子(上下文);
}
}
BehavigateConext类中注释掉的代码行是如何正常启用自动迁移的,但是我不知道如何使用自定义初始值设定项执行此操作,现在我在尝试从MigrateDatabaseToLatestVersion继承时收到显示的错误
有人有什么想法吗
注意:我已在软件包管理器中启用了自动迁移。出现此特定错误的原因是,迁移文件夹中的Configuration.cs文件(在PM Console中运行
启用迁移后创建的文件)设置为内部密封。因此,它不能被继承,因为密封的
类根据定义是不可继承的
我建议将种子方法实际移动到Configuration.cs文件中,并通过PM使用updatedatabase
生成数据库。这更符合正常的EF实践
然后,如果要启用自动迁移,请添加行AutomaticMigrationsEnabled=true代码>到您的配置
初始值设定项