2012-08-02 25 views
11

說,我們使用EF代碼首先,我們有這個簡單的模型:EF代碼優先5.0.rc遷移不`噸更新標識屬性

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代碼首先爲我們創建表的預期:

enter image description here enter image description here



那麼,假設我們犯了一個錯誤,而事實上,我們希望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"); 
     } 
    } 
} 

Import and part is:

AlterColumn(「dbo.CUsers」,「ID」,c => c.Int(nullable:false,identity:true));

因此,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指令。所以,因爲這存在一個問題:

(更新數據庫) enter image description here enter image description here

因此,用戶是主要的結束,現在,已經有ID身份:「YES」的標誌,但身份仍是「沒有」。地址是依賴端,必須有ID標識「NO」,但仍然是「YES」。所以我無法添加新的用戶到用戶表,因爲沒有爲新實例生成新的ID。

如果我刪除整個數據庫,EF Code First將從頭開始正確創建新表,所以這只是Migrations的一個問題。

在這種情況下我該怎麼辦?這是EF遷移錯誤?

回答

22

我不確定它是否是一個錯誤,因爲存在另一個問題 - 您cannot alter existing column to identity or remove identity。我可以想象,這被認爲是完全手動遷移,以明確您必須移動數據。

+1

哇,真是太遺憾了。沒知道。謝謝您的回答。 – Roman 2012-08-03 13:11:49

+1

返回蜱沒有報價 – d512 2014-10-02 19:03:35