实体框架优先:通过更新数据库进行迁移失败,强制进行不必要的(?)add-migration [英] Entity Framework code-first: migration fails with update-database, forces unneccessary(?) add-migration

查看:236
本文介绍了实体框架优先:通过更新数据库进行迁移失败,强制进行不必要的(?)add-migration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



删除时数据库并调用update-database,我得到以下错误:


由于存在$ b $,因此无法更新数据库以匹配当前模型b正在进行更改,并且禁用了自动迁移。将待定模型更改
写入基于代码的迁移,或者启用
自动迁移。将
DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true可启用
自动迁移。您可以使用Add-Migration命令将
待处理的模型更改写入基于代码的迁移中。


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



但是由于我不想 AutomaticMigrationsEnabled ,我还尝试过再次删除数据库,分别是 update-database add-migration 。我最后进行了一次额外的迁移,似乎没有任何改变(请参阅下文)。我还尝试将这些行添加到初始迁移的底部-但这并没有改变。



其中一个模型:

  [Table(Speaker.TABLENAME)] 
公共类议长:BaseModel
{
public const String TABLENAME = Speaker;

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

[必需]
[MaxLength(50,ErrorMessage =名称必须小于等于50个字符)]
公共字符串名称{组; }
}

初始迁移代码:

 公共部分类InitialCreate:DbMigration 
{
公共重写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。名称,正确,错误); // //手动添加:唯一名称
// [...]
}
}

内部密封类配置:DbMigrationsConfiguration< MyProject.Repositories.DBContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}

受保护的重写void Seed(MyProject.Repositories.DBContext上下文)
{
// ...
}
}

下面是add-migration创建的代码:它似乎没有做任何新的事情-也许我是

 公共部分类UnneccessaryMigration:DbMigration 
{
公共重写void Up()
{
// //这与InitialMigrations是否完全相同?
AlterColumn( dbo.Speaker, Id,c => c.Guid(nullable:false,identity:true));
// ...
}

公共替代无效Down()
{
// ...
AlterColumn( dbo .Speaker, Id,c => c.Guid(nullable:false));
}
}

所以我很好奇:我做了些什么来迷失方向迁移?而我该怎么做才能使其仅进行一次初始迁移?



解决方案:以下解决方法对我有用:


  1. 我删除了数据库,并按照此处的描述进行了所有迁移:
    https://stackoverflow.com/a/11679386/3168401

  2. 已执行的启用迁移+初始添加迁移

  3. 将我手工制作的.Index()更改合并到文件中。
    现在,更新数据库可以再次工作-删除数据库时也可以重复。


解决方案

<我还尝试过再次删除数据库,即更新数据库,然后删除
进行添加迁移。我结束了一次额外的迁移,似乎
不更改任何内容(请参见下文)


根据上述详细信息,我认为你先做最后一件事。如果您在添加迁移之前运行更新数据库,它将不会使用您的迁移模式来更新数据库。首先,您需要添加迁移,然后运行更新命令。



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

  PM> Enable-migrations //您已经不需要了,因为您已经完成了
PM> Add-migration Give_it_a_name
PM>更新数据库


I have a funny effect using migration (EF 5.0) and code-first:

I created some models with GUID primary keys. (BTW: It is important for me, that SQL Server uses NEWSEQUENTIALID(), which seems to be the default value in the current version)

At some point I activated migrations. I added some code to the initial migration, this is mostly .Index() as needed.

When I delete the database and call update-database, I get the following error:

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.

I tried AutomaticMigrationsEnabled = true, which worked without changing or adding anything!

But since I don't want AutomaticMigrationsEnabled, 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). I also tried adding these lines to the bottom of the initial migration - but this does not change anything.

One of the models:

[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; }
}

The initial migration code:

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)
    {
        // ...
    }
}

Below is the code created by add-migration: It does not seem to do anything new - maybe I am missing something?

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));
    }
}

So I am curious: What did I do to disorientate migrations? And what can I do to get it working with just one initial migration?

Solution: The following workaround did it for me:

  1. I deleted the database and all migrations as decribed here: https://stackoverflow.com/a/11679386/3168401
  2. Executed Enable-Migrations + Add-Migration Initial
  3. Merged my handmade .Index() changes into the file. Now Update-Database works again - also repeatedly, when deleting the database.

解决方案

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)

Based on above details, I think you have done last thing first. If you run Update database before Add-migration, it won't update the database with your migration schemas. First you need to add the migration and then run update command.

Try them in this order using package manager console.

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

这篇关于实体框架优先:通过更新数据库进行迁移失败,强制进行不必要的(?)add-migration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆