c# - Entity Framework 代码优先 : migration fails with update-database, 强制不必要的(?)添加迁移

标签 c# entity-framework entity-framework-migrations

我使用迁移 (EF 5.0) 和代码优先产生了有趣的效果:

我使用 GUID 主键创建了一些模型。 (顺便说一句:对我来说很重要的是,SQL Server 使用 NEWSEQUENTIALID(),这似乎是当前版本中的默认值)

在某个时候我激活了迁移。我在初始迁移中添加了一些代码,这主要是根据需要的 .Index()

当我删除数据库并调用 update-database 时,出现以下错误:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration.

我尝试了AutomaticMigrationsEnabled = true,它无需更改或添加任何内容即可工作!

但由于我不想要 AutomaticMigrationsEnabled,我还尝试再次删除数据库,调用 update-database ,然后 add-migration 。我最终进行了一次额外的迁移,似乎没有改变任何东西(见下文)。我还尝试将这些行添加到初始迁移的底部 - 但这不会改变任何内容。

模型之一:

[Table(Speaker.TABLENAME)]
public class Speaker : BaseModel
{
    public const String TABLENAME = "Speaker";

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    [Required]
    [MaxLength(50, ErrorMessage = "Name must be 50 characters or less")]
    public string Name { get; set; }
}

初始迁移代码:

public partial class InitialCreate : DbMigration
{
    public override void Up()
    {
        // [...]
        CreateTable(
            "dbo.Speaker",
            c => new
                {
                    Id = c.Guid(nullable: false, identity: true),
                    Name = c.String(nullable: false, maxLength: 50),
                })
            .PrimaryKey(t => t.Id)
            .Index(t => t.Name, true, false);   // added manually: unique Name
        // [...]
    }
}

internal sealed class Configuration : DbMigrationsConfiguration<MyProject.Repositories.DBContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(MyProject.Repositories.DBContext context)
    {
        // ...
    }
}

下面是 add-migration 创建的代码:它似乎没有做任何新的事情 - 也许我错过了一些东西?

public partial class UnneccessaryMigration : DbMigration
{
    public override void Up()
    {
        // isn't this the exact same code from InitialMigrations?
        AlterColumn("dbo.Speaker", "Id", c => c.Guid(nullable: false, identity: true));
        // ...
    }

    public override void Down()
    {
        //...
        AlterColumn("dbo.Speaker", "Id", c => c.Guid(nullable: false));
    }
}

所以我很好奇:我做了什么让迁移迷失方向?我该怎么做才能使其仅通过一次初始迁移就可以工作?

解决方案:以下解决方法为我完成了:

  1. 我删除了数据库和所有迁移,如下所述: https://stackoverflow.com/a/11679386/3168401
  2. 已执行 Enable-Migrations + Add-Migration Initial
  3. 将我手工制作的 .Index() 更改合并到文件中。 现在,更新数据库再次工作 - 删除数据库时也会重复工作。

最佳答案

I also tried deleting the database again, called update-database and then add-migration. I ended up with an additional migration that seems not to change anything (see below)

根据以上详细信息,我认为您首先完成了最后一件事。如果您在 Add-migration 之前运行更新数据库,它不会使用您的迁移架构更新数据库。首先您需要添加迁移,然后运行更新命令。

使用包管理器控制台按此顺序尝试它们。

PM> Enable-migrations //You don't need this as you have already done it
PM> Add-migration Give_it_a_name
PM> Update-database

关于c# - Entity Framework 代码优先 : migration fails with update-database, 强制不必要的(?)添加迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20968520/

相关文章:

entity-framework - EF5 迁移 - 删除约束时重复/重新定义的变量错误 |使用 SQL GO 命令的问题

c# - 在内存方面,将一个长的非动态字符串存储为单个字符串对象还是让程序从重复部分构建它更好?

sql - 关于 LINQ 最高性能方法的思考

C# 无法从类访问属性

c# - 如果同一实体包含在实体的不同集合中,如何更改实体的状态

.net - Entity Framework 4.1:如何按对象ID删除

c# - 如何在 EF 迁移中使用 SqlResource 方法?

entity-framework - 如何将 SQL Server View 与 EF Code First 迁移结合使用

c# - 如何检查是否未分配 DateTime 对象?

c# - Azure 表客户端 ListTablesSegmented