EF代码优先5.0.rc迁移不`吨更新标识属性(EF Code First 5.0.rc Migra

2019-09-20 16:38发布

比方说,我们使用EF代码首先,我们有这个简单的模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace EFCodeFirstIdentityProblem.Models
{
    public class CAddress
    {
        public int ID { get; set; }

        public string Street { get; set; }
        public string Building { get; set; }

        public virtual CUser User { get; set; }
    }

    public class CUser
    {
        public int ID { get; set; }

        public string Name { get; set; }
        public string Age { get; set; }

        [Required]
        public virtual CAddress Address { get; set; }
    }

    public class MyContext : DbContext
    {
        public DbSet<CAddress> Addresses { get; set; }
        public DbSet<CUser> Users { get; set; }
    }
}


与此类似, CAddress将是这1结束本金 :0..1关系。 接下来,我们连接字符串添加到Web.config中(我使用MSSQL 2008 R2),请使用此模式控制器,运行。 EF代码首先为我们创建表的预期:



所以,让我们假设我们犯了一个错误,而事实上,我们希望CUser能成为其中的主要 0..1结束:1的关系。 因此,我们做出改变:

        ...
        [Required]
        public virtual CUser User { get; set; }
        ...

        ...
        public virtual CAddress Address { get; set; }
        ...

建立,那么在包管理器控制台运行,并添加一些迁移:

PM> Enable-Migrations
Checking if the context targets an existing database...
Detected database created with a database initializer. Scaffolded migration '201208021053489_InitialCreate' corresponding to existing database. To use an automatic migration instead, delete the Migrations folder and re-run Enable-Migrations specifying the -EnableAutomaticMigrations parameter.
Code First Migrations enabled for project EFCodeFirstIdentityProblem.
PM> Add-Migration ChangeDependency
Scaffolding migration 'ChangeDependency'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration 201208021157341_ChangeDependency' again.
PM> 

这里we`ve被赋予了“ChangeDependency”迁移的内容:

namespace EFCodeFirstIdentityProblem.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class ChangeDependency : DbMigration
    {
        public override void Up()
        {
            DropForeignKey("dbo.CUsers", "ID", "dbo.CAddresses");
            DropIndex("dbo.CUsers", new[] { "ID" });
            AlterColumn("dbo.CAddresses", "ID", c => c.Int(nullable: false));
            AlterColumn("dbo.CUsers", "ID", c => c.Int(nullable: false, identity: true)); //identity: true - this is important
            AddForeignKey("dbo.CAddresses", "ID", "dbo.CUsers", "ID");
            CreateIndex("dbo.CAddresses", "ID");
        }

        public override void Down()
        {
            DropIndex("dbo.CAddresses", new[] { "ID" });
            DropForeignKey("dbo.CAddresses", "ID", "dbo.CUsers");
            AlterColumn("dbo.CUsers", "ID", c => c.Int(nullable: false));
            AlterColumn("dbo.CAddresses", "ID", c => c.Int(nullable: false, identity: true));
            CreateIndex("dbo.CUsers", "ID");
            AddForeignKey("dbo.CUsers", "ID", "dbo.CAddresses", "ID");
        }
    }
}

Importand部分是:

AlterColumn( “dbo.CUsers”, “ID”中,c => c.Int(可为空的:假, 身份:真 ));

所以CUsers.ID现在必须在DB成为身份。 让我们犯这个更改为DB:

PM> 
PM> Update-Database -Verbose
Using StartUp project 'EFCodeFirstIdentityProblem'.
Using NuGet project 'EFCodeFirstIdentityProblem'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'EFTest' (DataSource: (local), Provider: System.Data.SqlClient, Origin: Configuration).
Applying code-based migrations: [201208021157341_ChangeDependency].
Applying code-based migration: 201208021157341_ChangeDependency.
ALTER TABLE [dbo].[CUsers] DROP CONSTRAINT [FK_dbo.CUsers_dbo.CAddresses_ID]
DROP INDEX [IX_ID] ON [dbo].[CUsers]
ALTER TABLE [dbo].[CAddresses] ALTER COLUMN [ID] [int] NOT NULL
ALTER TABLE [dbo].[CUsers] ALTER COLUMN [ID] [int] NOT NULL
ALTER TABLE [dbo].[CAddresses] ADD CONSTRAINT [FK_dbo.CAddresses_dbo.CUsers_ID] FOREIGN KEY ([ID]) REFERENCES [dbo].[CUsers] ([ID])
CREATE INDEX [IX_ID] ON [dbo].[CAddresses]([ID])
[Inserting migration history record]
Running Seed method.
PM> 

还有,成为标识列在DB CUsers.ID的迁移没有给出SQL指令。 所以,因为这是有问题的:

(更新数据库)

因此,用户是主要的结束,现在,已经有ID身份:“YES”的标志,但身份仍是“NO”。 和地址依赖年底,已拥有ID身份“NO”,但仍然是“YES”。 所以,我可以将新的用户不能添加到用户表,因为新的实例,不产生新的ID。

如果我把整个数据库,EF代码首先创建从头开始正常新表,所以这只是一个迁移问题。

我怎么在这种情况下怎么办? 这是EF迁移错误?

Answer 1:

我不知道这是否是一个错误,因为还有另外一个问题-你不能改变现有列身份或删除身份 。 我可以想像,这被认为是全手动迁移要清楚,你必须移动数据。



文章来源: EF Code First 5.0.rc Migrations doesn`t update Identity property