i working on c# project using entity framework 6
i want update-database have error after 'running seed method'
unable determine principal end of 'cookerapi.models.category_recipe_recipe' relationship. multiple added entities may have same primary key.
here's part of seed method:
context.recipes.addorupdate(x => x.id_recipe, new recipe() { id_recipe = 1, id_user = 1, id_category_main = 1, name_recipe = "zupa z kurek", rate = 0, level = "Łatwe", date_recipe = datetime.now, url_photo = "test", time = 45, number_person = 4, steps = 4, instruction = "test" } ); context.categories_recipes.addorupdate(x => x.id_category_recipe, new category_recipe() { id_category_recipe = 1, id_recipe = 1, id_category = 1 } );
category_recipe model:
public class category_recipe { [key] [databasegenerated(databasegeneratedoption.identity)] public int id_category_recipe{ get; set; } public int id_category { get; set; } public int id_recipe { get; set;} [foreignkey("id_category")] public category category { get; set; } [foreignkey("id_recipe")] public recipe recipe { get; set; } }
recipe model:
public class recipe { [key] [databasegenerated(databasegeneratedoption.identity)] public int id_recipe { get; set; } public int id_user { get; set; } public int id_category_main { get; set; } public string name_recipe { get; set; } public int rate { get; set; } //rate 0-5 public string level { get; set; } public datetime date_recipe { get; set; } //date of create public string url_photo { get; set; } //url of thumbnail public int time { get; set; } // in minutes public int number_person { get; set; } // recipe number of people public int steps { get; set; } public string instruction { get; set; } [foreignkey("id_user")] public user user { get;set;} [foreignkey("id_category_main")] public category_main category_main { get; set; } public icollection<category_recipe> categories_recipes { get; set; } public icollection<comment> comments { get; set; } public icollection<element> elements { get; set; } public icollection<rate> rates { get; set; } }
where problem , how can fix ?
solution:
context.recipes.addorupdate(x => x.id_recipe, new recipe() { id_recipe = 1, id_user = 1, id_category_main = 1, name_recipe = "zupa z kurek", rate = 0, level = "Łatwe", date_recipe = datetime.now, url_photo = "test", time = 45, number_person = 4, steps = 4, instruction = "test" } );
replace with:
context.categories_recipes.add(new category_recipe() { id_recipe = 3, id_category = 3 }); context.savechanges();
Comments
Post a Comment