c# - need guidelines in creating/updating related entities (is it possible with automapper?) -
i'm new in asp.net mvc , love improve here. used asp.net mvc + ef code first approach. i'm little confuse on how create/update related entites. here's scenario. say,
public class item { public int id { get; set; } public string name { get; set; } public virtual icollection<stock> stocks { get; set; } } public class stock { public int id { get; set; } public int itemid { get; set; } public int storageid { get; set; } public float amount { get; set; } public virtual item item { get; set; } public virtual storage storage { get; set; } } public class storage { public int id { get; set; } public name { get; set; } public virtual icollection<stock> stocks { get; set; } } so item has 1:many relationship stock. , storage has 1:many relationship stock
in displaying them used automapper worked perfectly. (thanks helping me)
now, i'm trying achieve is.. how create/update entites? (is possible used automapper here?)
say, in 1 post add item, stock, , selected storage. sample code great reference.
any appreciated. thanks
automapper tool map properties of view model to/from domain model.
the view model use in of views, , domain model underlying business model shouldn't exposed views.
this automapper simplifies, maps properties of these 2 models don't have keep on converting 1 model other.
now moving on creating / updating related entities...
say want add new stock using navigation property on item.
item item = this.dbsource.items.first(itementity => itementity.id == 5); if(item.stocks == null) item.stocks = new collection<stock>(); item.stocks.add(new stock { storageid = 3, amount = 123f }); this.dbsource.savechanges(); another case pointed out having new item , x amount of stock of item, want store in database in single operation.
storage storage = this.dbsource.storages.first(storageentity => storageentity.id == 3); if(storage.stocks == null) storage.stocks = new collection<stock>(); stock stock = new stock { storageid = 3, amount = 123f, item = new item { name = "redbull" } }; storage.stocks.add(stock); this.dbsource.savechanges(); or if have no data in database , want 3 models posted in single go...
stock stock = new stock { amount = 123f, item = new item { name = "redbull" } }; storage storage = new storage { name = "it's secret" }; storage.stocks.add(stock); this.dbsource.storages.add(storage); this.dbsource.savechanges(); also modify of models constructor initializes collection on of icollection navigational properties, way can avoid nullreferenceexception
so example modify item class this
public class item { public item() { this.stocks = new collection<stock>(); } public int id { get; set; } public string name { get; set; } public virtual icollection<stock> stocks { get; set; } } using item root property
collection<stock> stocks = new collection<stock>(); collection<stock> stocks.add(new stock { storageid = 123, amount = 1000f }); item item = new item { name = "pizza", stocks = stocks }; this.dbsource.savechanges();
Comments
Post a Comment