简体   繁体   中英

Entity framework trouble

I need to setup a one-to-many relationship with entity framework.

I am trying to build a recipe, but a recipe CAN be composed of recipes.

How to achieve that with EF ?

public class Recipe
{
    public Recipe()
    {
        Deleted = false;

        Recipes = new List<Recipe>();
    }

    [Key]
    public int RecipeId { get; set; }

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

I don't need the whole recursive thing(parent, child) only the child I'm interested in. Is it feasible using EF ? Can anyone point me to the right direction

Ex:

Recipe A

Recipe B => A

Recipe C

Recipe D => B

Recipe E => B, C

This will be a many-to-many relationship, because each recipe can have multiple and multiple parent recipes. When you create your child recipes, you will have to assign a parent to it, so you must define the parent relationship as well.

You could try the InverseProperty dataannotation, for more info check out this question: Entity Framework 4.1 InverseProperty Attribute

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