简体   繁体   中英

Setting up Models for related data MVC 5/EF6

I have been trying to work this out for a while now and can't find an answer that makes sense to me. The concept is very common, so I must be totally misunderstanding a basic concept. If I have a recipe class that can be found in many recipe categories then I have;

public class Recipe
{
    public int ID { get; set; }
    public string Title { get; set; }

    public int CategoryID { get; set; }

    public virtual ICollection<Category> Categories { get; set; }
}

public class Category
{
    public int ID { get; set; }
    public string Description { get; set; }

    public int RecipeID {get; set;
    public virtual void Recipe Recipe {get; set;}

But I also need a join table that relates a recipe to a Category. I want to display this;

Recipe Title | Category

Mac-N-Cheese | Pasta | Easy

Pot Roast | Beef | Slow cooker

The Category is a table of available categories. So the join table has

RecipeID | CategoryID

I tried setting up the models using the Entity Framework format of foreign keys and navigation properties.

So I set up the join table like this; public class RecipeCategories { public int ID { get; set; } public int RecipeID { get; set; } public int CategoryID { get; set; }

    public virtual ICollection<Recipe> Recipes { get; set; }
    public virtual ICollection<Category> Categories { get; set; }
}

So a recipe can have many categories. The join table can have many recipes and many categories. The category table is just a simple list of categories. What am I missing? When I try to run the view there is no list of categories for the given recipe. The best I have achieved is the CategoryID.

Sorry for the long post, but you need all the details.

The problem is your Category model. The way you've currently defined it you have a one-to-many relationship between Category and Recipe (a Recipe can have many Categories but a Category one has a single Recipe). What you want is a many-to-many relationship so put a collection of Recipes on the Category and EF should automatically generate the join table.

public class Recipe
{
    public int ID { get; set; }
    public string Title { get; set; }

    public int CategoryID { get; set; }

    public virtual ICollection<Category> Categories { get; set; }
}

public class Category
{
    public int ID { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Recipe> Recipes { get; set; }
}

I am no getting actual scenario as you trying to achieve.

as you mention your model and then what you expected both contradict. as per you mention you have recipe and category (one to many) but later you change your model and mention you want (many to many) so you need join table. as a relation model you can handle 2 ways in EF. 1st without creating separate table and keep collection map to each model and 2nd way creating separate table and explicitly map that in your model. you need to explicitly specify while model building.

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Entity<Recipe>() .HasMany(c => c.Categories).WithMany(i => i.Recipes) .Map(t => t.MapLeftKey("RecipeID") .MapRightKey("CategoryID") .ToTable("ReceipeCategory") ); 

you can define your class model (and third table will generate but don't required specific model )

class Recipe {.... public virtual ICollection<Category> Categories { get; set; }
class Category {... public virtual ICollection<Recipe> Recipes { get; set; } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM