简体   繁体   English

使用EF Code-first和迁移的专有ID值

[英]Excplicit id values using EF Code-first and migrations

I develop my ASP.NET MVC4 app using EF Code-first, also im using Migrations feature. 我使用EF Code-first开发了我的ASP.NET MVC4应用程序,同时也使用了迁移功能。 I have specific entity and i want to set explicit id values for some reasons. 我有特定的实体,出于某些原因,我想设置明确的ID值。 How can i do this? 我怎样才能做到这一点? I tried to mark id property with attribute like this: 我试图用这样的属性标记id属性:

public class NewsSource
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }

    public string Title { get; set; }

    public string WebSiteUrl { get; set; }
}

Then i add corresponding migration, delete database and try to update database using Seed() method like this: 然后我添加相应的迁移,删除数据库并尝试使用Seed()方法更新数据库,如下所示:

context.NewsSources.AddOrUpdate(x => x.Title,
            new NewsSource {Id = 654, Title = "ABC", WebSiteUrl = @"http://www.abc.com/"},
            new NewsSource {Id = 22, Title = "XYZ", WebSiteUrl = @"http://XYZ.ru/"});

And ive got this error: "Cannot insert explicit value for identity column in table 'NewsSources' when IDENTITY_INSERT is set to OFF". 而我得到了这个错误:“当IDENTITY_INSERT设置为OFF时,无法在表'NewsSources'中为标识列插入显式值”。 How can i set identity_insert to ON using EF and migrations. 如何使用EF和迁移将identity_insert设置为ON。 As i understood from many topics its impossible and i have to use direct SQL-commands. 从许多主题可以理解,这是不可能的,因此我必须使用直接的SQL命令。 Am i wrong? 我错了吗?

I had a problem when I tried to change a column to an IDENTITY field when it was originally not an identity field. 当我尝试将列最初更改为IDENTITY字段时,我遇到了问题。 See this question and this one . 看到这个问题这个 You have to remove the column and recreate it to remove IDENTITY so fixing up foreign keys etc is probably a step too far for Entity Framework to do in a migration. 您必须删除该列并重新创建它以删除IDENTITY,因此,修复外键等对于Entity Framework进行迁移而言可能太过分了。 You will have to alter the Up() and Down() methods yourself, or you may get away with doing it in the database. 您将必须自己更改Up()和Down()方法,否则您可能无法在数据库中这样做。

What happens if you remove the DatabaseGenerated attribute and just leave the [Key] attribute? 如果删除DatabaseGenerated属性并仅保留[Key]属性会怎样?

public class NewsSource
{
    [Key]
    public int Id { get; set; }
    public string Title { get; set; }
    public string WebSiteUrl { get; set; }
}

I ran into this issue awhile back and I believe this was a work around for me. 我前不久遇到了这个问题,我认为这对我来说是个解决方法。 I can't test it now since I don't have any projects in front of me with EF in them. 我现在无法测试它,因为我面前没有任何带有EF的项目。 All I can say is EF code first and the nuget migrations package is a great thing, if you treat it the way you are supposed to treat it. 我只能说是EF代码,而nuget migrations软件包是一件很棒的事情,如果您以应有的方式对待它。 I have gotten myself into a number of WTF moments when using the two together and trying to seed complex related models. 当将两者一起使用并尝试播种复杂的相关模型时,我陷入了很多WTF时刻。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM