2014-09-04 65 views
0

我們有一個ASP.Net MVC4項目,現在一直在使用Entity Framework 5。我們剛剛使用NuGet包管理器更新到Entity Framework 6,現在我們在遷移代中出現了奇怪的行爲。EF5 - > EF6奇怪的遷移行爲

多對多表正確存在,現在EF想要添加額外的列和FK ...並且具有[NotMapped]屬性的列不會被忽略。我不得不使用.Ignore()

下面是許多對許多一直與EF5,但突然不希望與EF6工作...

public class Grade 
{ 
    [Key] 
    public int GradeKey { get; set; } 
    [Required] 
    public string GradeLevel { get; set; } 

    public virtual ICollection<Test> Tests { get; set; } 
} 

public class Test 
{ 
    [Key] 
    [HiddenInput(DisplayValue = false)] 
    public int TestKey { get; set; } 

    ... 

    [UIHint("_GradeCheckboxes")] 
    [Order(5)] 
    [Display(Order = 5)] 
    [DisplayName("Grades")] 
    public virtual ICollection<Grade> Grades { get; set; } 
} 

//Fluent API for the join 
modelBuilder.Entity<Grade>().HasKey(p => p.GradeKey).HasMany(p => p.Tests).WithMany(d => d.Grades).Map(x => 
{ 
    x.MapLeftKey("Grade_GradeKey"); 
    x.MapRightKey("Test_TestKey"); 
    x.ToTable("GradeTests"); 
}); 

// Migration 
public override void Up() 
{ 
    AddColumn("dbo.Grades", "Test_TestKey", c => c.Int()); // This is a problem 
    AlterColumn("dbo.Students", "StateCode", c => c.String()); // This already exists in DB 
    AlterColumn("dbo.Students", "SchoolName", c => c.String()); // This already exists in DB 
    CreateIndex("dbo.Grades", "Test_TestKey"); // Problem 
    CreateIndex("dbo.StudentTests", "StudentKey"); 
    CreateIndex("dbo.StudentTests", "TestKey"); 
    AddForeignKey("dbo.Grades", "Test_TestKey", "dbo.Tests", "TestKey"); // Problem 
    AddForeignKey("dbo.StudentTests", "StudentKey", "dbo.Students", "StudentKey", cascadeDelete: true); // Already exists 
    AddForeignKey("dbo.StudentTests", "TestKey", "dbo.Tests", "TestKey", cascadeDelete: true); // Already exists 
} 

有沒有人在培訓相關的代碼否則在升級時或使用EF6時會遇到此問題。我嘗試了許多不同的策略和互聯網搜索,試圖解決這個問題幾天。我甚至刪除了__MigrationHistory表並刪除了所有遷移,並且僅從模型中重新開始,但仍希望在Grades表中創建Test_TestKey列,而不是在多對多表中創建...

+0

我從來沒有使用數據的註釋和流暢的API在一起,事情是爲我的項目,從EF5遷移到EF6(流利API是更好的)總是工作。 – mostruash 2014-09-04 19:10:28

回答

0

我從甲級降到[鍵]屬性,測試和工程作爲intialising一個基本的數據庫

public class Grade 
{ 

    public int GradeKey { get; set; } 
    [Required] 
    public string GradeLevel { get; set; } 

    public virtual ICollection<Test> Tests { get; set; } 
} 

流利的API相同asyours建立多對多的表時,當預計

modelBuilder.Entity<Grade>() 
        .HasKey(g => g.GradeKey) 
        .HasMany(g => g.Tests) 
        .WithMany(t => t.Grades) 
        .Map(map => 
         { 
          map.ToTable("GradeTests"); 
          map.MapLeftKey("GradeKey"); 
          map.MapRightKey("TestKey"); 
         }); 

個數據庫表

enter image description here