2012-08-02 17 views
11

कहो, हम एफई कोड पहले उपयोग कर रहे हैं और हम इस साधारण मॉडल है:एफई कोड पहले 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 संबंध। अगला हम वेब पर कनेक्शन स्ट्रिंग जोड़ते हैं। कॉनफिग (मैं एमएसएसएलएल 2008 आर 2 का उपयोग करता हूं), इस मॉडल का उपयोग करने वाले नियंत्रक को चलाएं। एफई कोड पहले अपेक्षा के अनुरूप हमारे लिए टेबल बनाता है:

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> 

यहाँ "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"); 
     } 
    } 
} 

आयात भाग है:

अल्टरकॉलम ("dbo.CUsers", "आईडी", c => c.Int (nullable: false, पहचान: सत्य));

तो CUsers.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> 

कोई एसक्यूएल CUsers.ID की माइग्रेशन डीबी में पहचान स्तंभ बनकर दिए गए निर्देशों का नहीं है। तो, इसी वजह से वहाँ एक समस्या है:

(अद्यतन डेटाबेस) enter image description here enter image description here

तो, उपयोगकर्ता प्रिंसिपल अंत है, और आईडी पहचान के लिए है: "हाँ" झंडा, लेकिन पहचान अब भी है "नहीं"। और पता निर्भर अंत है, आईडी पहचान "नहीं" है, लेकिन अभी भी "हाँ" है। इसलिए मैं उपयोगकर्ता तालिका में नया उपयोगकर्ता नहीं जोड़ सकता, क्योंकि नए आईडी के लिए नई आईडी जेनरेट नहीं की गई है।

यदि मैं पूरा डेटाबेस छोड़ देता हूं, तो ईएफ कोड पहले स्क्रैच से नई टेबल तैयार करता है, इसलिए यह केवल माइग्रेशन की समस्या है।

मैं इस स्थिति में क्या करूँ? क्या यह ईएफ माइग्रेशन बग है?

उत्तर

22

मुझे यकीन नहीं है कि यह एक बग है क्योंकि एक और समस्या है - आप cannot alter existing column to identity or remove identity। मैं कल्पना कर सकता हूं कि इसे स्पष्ट करने के लिए इसे पूरी तरह से मैन्युअल माइग्रेशन माना जाता है कि आपको डेटा ले जाना चाहिए।

+1

वाह, यह शर्म की बात है। उसे नहीं पता था। आपके उत्तर के लिए धन्यवाद। – Roman

+1

बैक टिक्स उद्धरण नहीं हैं – d512