2012-04-14 23 views
6

Tôi có bốn thực thể mà tôi muốn dịch thành các bảng cơ sở dữ liệu thông qua mã api thông thạo đầu tiên (tôi đang sử dụng mô hình được tìm thấy tại databaseanswers.org), nhưng tôi không chắc chắn như thế nào. Vấn đề tôi gặp phải là SuggestedMenuId đang được di chuyển qua hai bảng khác nhau trong một khóa Composite (MenuCourse và CourseRecipeChoice).Mã API lưu loát đầu tiên và các thuộc tính điều hướng trong một bảng tham gia

Dưới đây là thông điệp tôi nhận được:

"Một hoặc nhiều lỗi xác nhận đã được phát hiện trong thế hệ người mẫu:

\ tSystem.Data.Entity.Edm.EdmAssociationConstraint:: Số tài sản trong Vai trò phụ thuộc và vai trò chính trong ràng buộc mối quan hệ phải giống nhau. "

Đây là những gì tôi đã cố gắng trong lớp EntityTypeConfiguration của tôi và nó rõ ràng là không chính xác ...

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

cú pháp chính xác cho các thuộc tính điều hướng và cú pháp chính xác cho cú pháp api thông thạo cho CourseRecipeChoice là gì tham gia bảng ?

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

Các phím như sau:

  • SuggestedMenu (Id)
  • MenuCourse (Id, SuggestedMenuId)
  • CourseRecipeChoice (Id, SuggestedMenuId, MenuCourseId, RecipeId) // đây thực sự là nơi tôi bị lẫn lộn vì theo mô hình, SuggestedMenuId là một PK trong SuggestedM enu và PF trong MenuCourse và CourseRecipeChoice (có thể đây chỉ là thiết kế xấu?)
  • Recipe (RecipeId)
+0

Bạn có thể cho tôi biết chìa khóa cho mỗi bàn là gì và chìa khóa nước ngoài là gì, tôi đoán nhưng không chắc chắn. Với không nên có vấn đề lớn hơn tôi nghĩ. – NSGaga

+0

@NSGaga Tôi chưa đọc câu trả lời của bạn, nhưng tôi đã cập nhật câu hỏi để thêm các phím ... – Robert

Trả lời

15

... dựa trên các thông tin trong tầm tay (phím, các mối quan hệ không hoàn toàn rõ ràng) ,
đây là kịch bản phức tạp nhất và nên bao gồm những gì bạn có thể có tôi nghĩ ...

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

... và trong OnModelCreating (tôi thích nó tất cả các cấu hình được thực hiện ở đó, mặc dù nó là giống nhau) .. .

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

... và để kiểm tra ví dụ: giống như ...

 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

Tôi sẽ cung cấp cho ảnh này và báo cáo lại càng sớm càng tốt. – Robert

+0

Nó hoạt động hoàn hảo! Cảm ơn bạn đã giúp tôi học điều gì đó mới mẻ ngay hôm nay! – Robert

+0

np nếu điều đó được giải quyết, bạn có thể đánh dấu/upvote câu trả lời, tốt nhất – NSGaga

Các vấn đề liên quan