如何在使用自定义数据库初始化器 c# 时启用自动迁移

How to enable automatic migration when using custom DB initializer c#

我已经创建了一个自定义数据库初始化器,这样我就可以重写种子方法并在每次使用 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 class 中注释掉的代码行是如何正常启用自动迁移的,但是我不知道如何使用我的自定义初始化程序执行此操作,现在我在尝试时收到显示的错误从 MigrateDatabaseToLatestVersion 继承。

有没有人有什么想法?

注意:我在包管理器中启用了自动迁移。

您收到该特定错误的原因是因为在迁移文件夹中的 Configuration.cs 文件中(在 PM 控制台中 运行 Enable-Migrations 之后创建)设置为 internal sealed。因此,它不能被继承,因为 sealed 类 根据定义是不可继承的。

我建议实际上将您的种子方法移动到您的 Configuration.cs 文件中,并通过 PM 使用 Update-Database 来生成您的数据库。这更符合正常的 EF 实践。

然后,如果您想启用自动迁移,请将行 AutomaticMigrationsEnabled = true; 添加到您的 Configuration 初始化程序。