2

I am creating an Asp.net MVC application in Which I have Recipe Model and RecipeCategory model. I want Recipe model contain foreign key of RecipeCategory model. I am new to Entity Framework. I am using code first approach of Entity framework. I have recipeCategory model defined as below:

public class RecipeCategory
{
    public int RecipeCategoryID { get; set; }
    public string Name { get; set; }
    public List<Recipe> Recipe { get; set; }
}

and I have recipe model class like below:

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

    public int RecipeCategoryID { get; set; }
    public RecipeCategory RecipeCategory { get; set; }
}

What I want:

  1. While creating new Recipe, I want list of Categories in UI to select RecipeCategories but I am unable to get that.

  2. I want properly foreign key established between both tables. please help me solve this

Delicate Hiba
  • 63
  • 1
  • 5
  • 13

3 Answers3

3

Your relationships are correct. For more improvement i would suggest plural name for your ICollection means Recipes instead of Recipe and replace List with ICollection.

public class RecipeCategory
{
    public int RecipeCategoryID { get; set; }
    public string Name { get; set; }
    public ICollection<Recipe> Recipes { get; set; }
}

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

    public int RecipeCategoryID { get; set; }
    public RecipeCategory RecipeCategory { get; set; }
}

Now come to main point as you have said you are unable to get RecipeCategories so can you please post your code for this which you are using?

It should be like this: 1. You must have created a context class inherited from DbContext and in that you must have defined your both entities in DbSets.

public class YourContext: DbContext
{
    public YourContext():
        base("yourConnectionString")
    {
    }

    public DbSet<Recipe> Recipes { get; set; }
    public DbSet<RecipeCategory> RecipeCategories { get; set; }
}

after that you can return your categories like this:

public IEnumerable<RecipeCategory> Get()
{
    YourContext context = new YourContext();
    return context.RecipeCategories;
}
Ankit Sahrawat
  • 1,306
  • 1
  • 11
  • 24
  • i have created view for Recipe Model and in the Create view, i do not have option for selecting category from the list? how can i do that if my relations are correct? – Delicate Hiba Jan 17 '17 at 06:13
  • i have to get separate RecipeCategories? i am creating view from strongly typed model Recipe? it should not be there in the create view? – Delicate Hiba Jan 17 '17 at 06:16
  • Okay.. So on your create action method you need to get the list of RecipeCategories from database and then store it in a ViewBag. Then on your view you can render the dropdown from the viewbag. Please take reference from http://stackoverflow.com/questions/16594958/how-to-use-a-viewbag-to-create-a-dropdownlist and http://www.compilemode.com/2016/01/bind-dropdownlist-using-viewbag-in-asp-net-mvc.html @DelicateHiba – Ankit Sahrawat Jan 17 '17 at 08:48
2

You need to put foreign key constraints in table.

[ForeignKey("ObjName")]

In your case,

public class Recipe
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string  Description { get; set; }
    [ForeignKey("RecipeCategory")]
    public int RecipeCategoryID { get; set; }
    public RecipeCategory RecipeCategory { get; set; }
}
Pavvy
  • 353
  • 3
  • 6
  • 15
0
public class RecipeCategory
{
 [Key][DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
    public int RecipeCategoryID { get; set; }
    public string Name { get; set; }
    public List<Recipe> Recipe { get; set; }
}


public class Recipe
{

    public int ID { get; set; }

    public virtual int RecipeCategoryID { get; set; }

    [ForeignKey("RecipeCategoryID ")]
    public virtual ApplicationUser ApplicationUser { get; set; }

    public string Title { get; set; }
    public string  Description { get; set; }

    public int RecipeCategoryID { get; set; }
    public RecipeCategory RecipeCategory { get; set; }
}
Mahesh Odedra
  • 59
  • 1
  • 7