2012-04-14 72 views
6

我有四個實體,我想通過代碼第一流利的API(我使用在databaseanswers.org找到的模型)轉換成數據庫表,但我不確定如何。我遇到的問題是SuggestedMenuId正在通過Composite鍵(MenuCourse和CourseRecipeChoice)中的兩個不同表遷移。代碼在連接表中的第一流利的API和導航屬性

這裏是我得到的消息:

「模型生成過程中檢測到一個或多個驗證錯誤:

\ tSystem.Data.Entity.Edm.EdmAssociationConstraint:在財產的數量關係約束中的依賴關係和主體角色必須完全相同。「

下面是我在EntityTypeConfiguration類已經試過和它的顯然是不正確......

public class CourseRecipeChoiceConfiguration : EntityTypeConfiguration<CourseRecipeChoice> 
{ 
    public CourseRecipeChoiceConfiguration() 
    { 
     HasKey(crc => new { crc.Id}); 
     HasRequired(r => r.Recipe).WithMany(crc => crc.CourseRecipeChoices).HasForeignKey(crc => crc.RecipeId); 
     HasRequired(m => m.MenuCourse).WithMany(crc => crc.CourseRecipeChoices).HasForeignKey(crc => crc.MenuCourseId); 
     HasRequired(m => m.MenuCourse).WithMany(crc => crc.CourseRecipeChoices).HasForeignKey(crc => crc.SuggestedMenu_MenuCourseId); 
    } 
} 

什麼是導航屬性的正確語法以及對CourseRecipeChoice流暢API語法正確的語法連接表?

public class SuggestedMenu 
{ 
    public int SuggestedMenuId { get; set; } 

    public virtual ICollection<MenuCourse> MenuCourses { get; set; } 
} 

public class MenuCourse 
{ 
    public int Id { get; set; } 
    public int SuggestedMenuId { get; set; } 

    public SuggestedMenu SuggestedMenu { get; set; } 
    public virtual ICollection<CourseRecipeChoice> CourseRecipeChoices { get; set; } 
} 

public class CourseRecipeChoice 
{ 
    public int SuggestedMenuId { get; set; } 
    public int MenuCourseId { get; set; } 
    public int Id { get; set; } 
    public int RecipeId { get; set; } 

    //How do I represent the navigation properties in this class? 

} 

public class Recipe 
{ 
    public int RecipeId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 

    public ICollection<CourseRecipeChoice> CourseRecipeChoices { get; set; } 
} 

鍵是如下:

  • SuggestedMenu(Id)的
  • MenuCourse(ID,SuggestedMenuId)
  • CourseRecipeChoice(ID,SuggestedMenuId,MenuCourseId,RecipeId) //這實際上是我感到困惑的地方,因爲根據模型,SuggestedMenuId是SuggestedM中的PK ENU和MenuCourse和CourseRecipeChoice(可能這只是糟糕的設計?)一個PF
  • 配方(RecipeId)
+0

你能告訴我什麼是每個表的關鍵和什麼是外鍵,我猜但不知道。我認爲不應該有更大的問題。 – NSGaga 2012-04-14 15:25:57

+0

@NSGaga我還沒有閱讀您的回覆,但我確實更新了問題以添加密鑰... – Robert 2012-04-14 18:43:26

回答

15

...根據手頭的信息(關鍵字,關係並不完全清楚) ,
這裏是最複雜的場景,並應涵蓋你可能有什麼我想......

public class SuggestedMenu 
{ 
    public int SuggestedMenuId { get; set; } 
    public string Description { get; set; } 
    public virtual ICollection<MenuCourse> MenuCourses { get; set; } 
    // public virtual ICollection<CourseRecipeChoice> CourseRecipeChoices { get; set; } 
} 
public class MenuCourse 
{ 
    public int MenuCourseId { get; set; } 
    public int SuggestedMenuId { get; set; } 
    public SuggestedMenu SuggestedMenu { get; set; } 
    public virtual ICollection<CourseRecipeChoice> CourseRecipeChoices { get; set; } 
} 
public class CourseRecipeChoice 
{ 
    public int CourseRecipeChoiceId { get; set; } 
    public int MenuCourseId { get; set; } 
    public int SuggestedMenuId { get; set; } 
    public int RecipeId { get; set; } 
    // no virtuals if required, non-optional 
    public Recipe Recipe { get; set; } 
    public MenuCourse MenuCourse { get; set; } 
    // public SuggestedMenu SuggestedMenu { get; set; } 
} 
public class Recipe 
{ 
    public int RecipeId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public virtual ICollection<CourseRecipeChoice> CourseRecipeChoices { get; set; } 
} 

...在OnModelCreating(我更喜歡它的所有配置方面所做的工作,但它是相同的).. 。

modelBuilder.Entity<CourseRecipeChoice>() 
    .HasKey(crc => new { crc.CourseRecipeChoiceId, crc.SuggestedMenuId, crc.MenuCourseId, crc.RecipeId }); 

modelBuilder.Entity<CourseRecipeChoice>() 
    .HasRequired(r => r.Recipe) 
    .WithMany(crc => crc.CourseRecipeChoices) 
    .HasForeignKey(crc => crc.RecipeId) 
    .WillCascadeOnDelete(false); 

modelBuilder.Entity<CourseRecipeChoice>() 
    .HasRequired(m => m.MenuCourse) 
    .WithMany(crc => crc.CourseRecipeChoices) 
    .HasForeignKey(crc => new { crc.MenuCourseId, crc.SuggestedMenuId }) 
    .WillCascadeOnDelete(false); 

modelBuilder.Entity<SuggestedMenu>() 
    .HasKey(crc => crc.SuggestedMenuId); 

modelBuilder.Entity<MenuCourse>() 
    .HasKey(crc => new { crc.MenuCourseId, crc.SuggestedMenuId }); 

modelBuilder.Entity<MenuCourse>() 
    .HasRequired(m => m.SuggestedMenu) 
    .WithMany(crc => crc.MenuCourses) 
    .HasForeignKey(crc => crc.SuggestedMenuId) 
    .WillCascadeOnDelete(false); 

modelBuilder.Entity<Recipe>() 
    .HasKey(crc => crc.RecipeId); 

...並測試例如像...

 using (var db = new YourDbContext()) 
     { 
      SuggestedMenu suggestedmenu = new SuggestedMenu { Description = "suggested menu" }; 
      var menucourse = new MenuCourse { MenuCourseId = 2, SuggestedMenu = suggestedmenu }; 
      var recipe = new Recipe { Name = "My recipe", Description = "recipe desc" }; 
      var crc = new CourseRecipeChoice { CourseRecipeChoiceId = 2, MenuCourse = menucourse, Recipe = recipe, }; 
      db.CourseRecipeChoices.Add(crc); 
      int recordsAffected = db.SaveChanges(); 
      foreach (var crcs in db.CourseRecipeChoices.Include(c => c.MenuCourse).Include(c => c.Recipe)) 
      { 
       Console.WriteLine("{0}, {1}, {2}, {3}", crcs.MenuCourse.MenuCourseId, crcs.MenuCourse.SuggestedMenuId, crcs.Recipe.Name, crcs.Recipe.Description); 
      } 
     } 
+0

我會給這個鏡頭並儘快報告。 – Robert 2012-04-14 18:46:54

+0

它工作完美!感謝您幫助我學習新的東西! – Robert 2012-04-14 19:02:48

+0

np如果解決了,您可以標記/ upvote答案,最好 – NSGaga 2012-04-14 19:04:39

相關問題