Code First Fluent API and Navigation Properties in a Join Table

后端 未结 1 971
攒了一身酷
攒了一身酷 2020-12-18 04:42

I have four entities that I would like to translate into database tables via code first fluent api (I\'m using a model found at databaseanswers.org), but I\'m not certain as

相关标签:
1条回答
  • 2020-12-18 05:03

    ...based on the info at hand (keys, relationships are not entirely clear),
    here is the most complex scenario and should cover what you might have I think...

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

    ...and in OnModelCreating (I prefer it all config done there, though it's the same)...

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

    ...and to test e.g. something like...

            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 讨论(0)
提交回复
热议问题