【问题标题】:How to enable automatic migration when using custom DB initializer c#如何在使用自定义 DB 初始化程序 c# 时启用自动迁移
【发布时间】: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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 2012-05-16
      • 2023-03-18
      • 2019-11-28
      • 1970-01-01
      相关资源
      最近更新 更多