c# - populate many-to-many relationship table using navigation properties in Entity Framework -
i learning entity framework in asp.net mvc application. have 3 models -
appmodel, categorymodel , app_categorymodel (to specify many many relationship between appmodel , categorymodel). snippet of is:
public class categorymodel { [key] public int id { get; set; } public string name {get; set; } public virtual icollection<app_categorymodel> mapping { get; set; } } public class appmodel { [key] public int id { get; set; } public string name { get; set; } public virtual icollection<app_categorymodel> mapping { get; set; } } public class app_categorymodel { [key] public int id {get; set;} public int appid {get; set; } public int categoryid {get; set; } public virtual appmodel app {get; set;} public virtual categorymodel category {get; set;} }
i following 'code-first' approach , tables got created successfully. but, stuck @ how populate , display information. have following input data: list<appmodel>
list<categorymodel>
, dictionary<"appname", list<categorymodel>>
how move on here can update mapping table?
also, wanted understand whether correct approach represent data. since app can have multiple categories - expect output collection of unique apps along list of categories each app, like:
dictionary<appmodel, list<categorymodel>>
edit: tried per suggestion smoksnes-
list<categorymodel> cat_list = new list<categorymodel>(); categorymodel c1 = new categorymodel(); c1.name = "string1"; cat_list.add(c1); categorymodel c2 = new categorymodel(); c2.name = "string2"; cat_list.add(c2); list<appmodel> app_list = new list<appmodel>(); appmodel a1 = new appmodel(); a1.name = "app1"; app_list.add(a1); appmodel a2 = new appmodel(); a2.name = "app2"; app_list.add(a2); a1.mapping.add(c1); a1.mapping.add(c2); a2.mapping.add(c1); a2.mapping.add(c2); db.categories.addrange(cat_list); db.apps.addrange(app_list); db.savechanges();
after this, ef worked expeted - 2 categories , 2 apps , 4 entries in mapping table.
although worked, not sure stopping ef create 4 entries categories?
just barry o´kane mentioned in comment there's no reason keep app_categorymodel
model. ef manage you. should keep if contains information regarding relation between 2 tables. according example, there's no reason keep it.
public class categorymodel { public categorymodel() { appmodels = new list<appmodel>(); } [key] public int id { get; set; } public string name {get; set; } public virtual icollection<appmodel> appmodels { get; set; } } public class appmodel { public appmodel() { // not sure if needed anymore. ef adds it. categorymodels = new list<categorymodel>(); } [key] public int id { get; set; } public string name { get; set; } public virtual icollection<categorymodel> categorymodels { get; set; } }
and regarding question representation, don't think it's necessary. since appmodel
has connected categorymodel
on it's model there's no reason dictionary
. can store in list<appmodel>
instead.
ilist<appmodel> myapps = context.appmodels.tolist(); foreach (var myapp in myapps) { console.writeline("app {0} has following categories:", myapp.id); foreach (var category in myapp.categorymodels) { console.writeline(category.name); } }
and when want add category app:
// don't know how create context, below it's called context. var newcategorymodel = new categorymodel { name = "so awesome!" }; var appmodel = context.appmodels.firstordefault(x => x.id == 1); appmodel.categorymodels.add(newcategorymodel); // ef automatically set foreign keys you... context.savechanges();
and if want make sure no category added twice:
public void addcategory(int appid, string categoryname) { using(var context = new dbcontext()) { var category = context.categorymodels.firstordefault(x => x.name == categoryname); if(category == null) { // create new categorymodel if doesn't exist. category = new categorymodel { name = categoryname }; } var appmodel = new appmodel { id = appid }; // attach save on db-trip context.appmodels.attach(appmodel); //todo: possibly check if particular appmodel has category? appmodel.categorymodels.add(category); context.savechanges(); } }
Comments
Post a Comment