2012-08-02 9 views
11

言って、我々はEFコードファーストを使用していて、私たちは、この単純なモデルがあります。このようEFコードファースト5.0.rc移行doesnの `tの更新アイデンティティプロパティ

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を使用)に接続文字列を追加し、このモデルを使用するコントローラを作成して実行します。

enter image description here enter image description here



そうで、我々はミスを犯したと仮定しましょう、そして実際に、私たちはCUserは、このの主要終わりにしたい:EFコードファーストは、予想通り、私たちのためにテーブルを作成します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> 

ここで何を "ChangeDependency" 移行のために与えられてwe`ve:

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

インポートと部分は:

AlterColumn( "dbo.CUsers"、 "ID"、c => c.Int(nullable:false、同一性:true));

だから、CUsers.IDはDB内でIDになる必要があります。

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のID列になるCUsers.IDの移行によるSQL命令はありません。だから、問題がある。このため:

(更新されたデータベース) のでenter image description here enter image description here

、ユーザーが主要エンドは今、およびIDのアイデンティティを持つことがあります:「YES」フラグが、アイデンティティはまだあります"いいえ"。アドレスは従属エンドであり、IDアイデンティティ「NO」を持っていなければならないが、依然として「YES」である。だから私は、新しいIDが新しいインスタンスのために生成されないので、新しいUser to Userテーブルを追加することができません。

データベース全体を削除した場合、EFコードファーストは最初から新しいテーブルを作成するので、これはマイグレーションの問題です。

この場合、私は何をしますか?このEF Migrationsはバグですか?

答えて

22

別の問題があるため、バグかどうかわかりません。cannot alter existing column to identity or remove identityです。これはデータを移動する必要があることを明確にするために完全に手動で移行すると考えられます。

+1

うわー、それは残念です。それを知らなかった。あなたの答えをありがとう。 – Roman

+1

バックティックは引用符ではありません – d512

関連する問題