newbie writing, I’m new to MVC 2 C# and using EntityFramework with LINQ.
In many examples (including the MVC 2 MusicStore of Microsoft) I’ve noticed that the Database updates are made in the Controllers layers. For example If I want to update a Database Product I call to UpdateModel(product,”Products” ) and call to SaveChanges() in the ProductController class
Ok, simple and easy but is this following MVC “traditional” pattern ??
Instead of this, I try to delegate the responsibility to the ProductModel class (I’ve made it with success in Adding and Deleting with AutoMapper) but I don’t find the way to update from the model
I tried something like
Mapper.CreateMap<Product, ProductModels>();
Mapper.CreateMap<ProductModels, Product>();
var p = Mapper.Map<Product, ProductModels>(prod);
productosBD.AttachTo("Products", p);
productosBD.SaveChanges();
but no way…
Could somebody tell me how can I update an entity in the model layer??
Thanks in advance
ASP NET MVC has evolved a lot from version 1 Beta to version 2 and now version 3 although 2 and 3 are much more similar.
A lot of such samples are related to MVC 1 – something I learnt the hard way. I am pretty new to ASP NET MVC but realised in MVC 2, ModelBinder looks after updating the model so normally if you define your method signature as your model, you do not need to call
UpdateModel.For example:
However if you define it like this, then you need to do that:
In terms of where the update happens, yes, it will happen in your controller but it depends if you use a Repository pattern or use the bare database context. I personally use a
ServiceProxywhich then talks to a WCF Service which talks to aManagerwhich uses aRepositoryto do that.