0

Whenever I update an entity, I need to include every field in the form as a hidden input, so that it's value doesn't get set to null.

Setting the property as not modified, like the code below, prevent this. But this code is abstracted into a generic IRepository interface encapsulated by an UnitOfWork class, so I can't specify non-modified properties for each entity.

Entry(entry.Entity as MyModel).Property(e => e.AProperty).IsModified = false;

Is there another way to do that?

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
  • 1
    Have you tried searching? Don't use entity models as viewmodels; create a separate viewmodel. – CodeCaster Feb 07 '15 at 16:00
  • Could you please explain how the use of ViewModels would help in here? I'm new to ASP.NET and I've been following tutorials so far, none mentioned viewmodel pattern yet, so I'm kinda lost, thanks in advance. – Christopher Francisco Feb 07 '15 at 16:05
  • See [ViewModels with asp.net mvc 4 and EntityFramework whats the Point](http://stackoverflow.com/questions/15391322/viewmodels-with-asp-net-mvc-4-and-entityframework-whats-the-point), reason 1. – CodeCaster Feb 07 '15 at 16:08
  • Ok so basically, according to `Avoid problems with your ORM tool's tracking abilities` EF ORM will try to update every property it finds in the object, so we use `ViewModel` so that those properties are limited to what you need to update, in this case? – Christopher Francisco Feb 07 '15 at 16:15
  • Well that's reason 2 :P But your conclusion is correct. You update the entity with the viewmodel's properties, only changing values that that view should change. – CodeCaster Feb 07 '15 at 16:16
  • OK so I am up to the point where my ViewModels are ready, but how do I save to the DbContext? The method accepts a `MyModel` but not `MyModelView` ? I'm afraid If I map each property from VM to M, I still will have the same error why I posted this question – Christopher Francisco Feb 07 '15 at 19:08

1 Answers1

0

You could use a MVVM architectural pattern. The Model View ViewModel (MVVM) is an architectural pattern used in software engineering originated from Microsoft as a specialization of the Presentation Model design pattern introduced by Martin Fowler.

Model : Model refers to your application data. In a real world ASP.NET web application, your data will typically be stored in a SQL Server database and your UI gets it from the server by making AJAX requests or some similar technique.
View Model : View Model refers to your data and UI level operations that you wish to perform on the data. The operations may include business validations or conditional checks or updating certain parts of the web page. You can think of View Model as a wrapper over your model data that adds UI level operations to it.
View : View is the user interface of your application. A View interacts with View Model to invoke some operations. A View is updated whenever data from the View Model changes. For a web application a view typically takes the form of an HTML document.

Here is a real world example using ASP.NET and EF. http://blog.tonysneed.com/2014/05/28/real-world-mvvm-with-entity-framework-and-asp-net-web-api/

Icaro Camelo
  • 382
  • 1
  • 10