[英]Avoid explicitly marking a property as not modified in Entity Framework
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
. 每当我更新实体时,都需要将表单中的每个字段都作为隐藏输入包括在内,这样它的值就不会设置为
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. 但是此代码被抽象到由
UnitOfWork
类封装的通用IRepository
接口中,因此我不能为每个实体指定未修改的属性。
Entry(entry.Entity as MyModel).Property(e => e.AProperty).IsModified = false;
Is there another way to do that? 还有另一种方法吗?
You could use a MVVM architectural pattern. 您可以使用MVVM体系结构模式。 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.
模型视图ViewModel(MVVM)是一种软件设计中使用的体系结构模式,该模式起源于Microsoft,是Martin Fowler引入的Presentation Model设计模式的专门技术。
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. 这是一个使用ASP.NET和EF的真实示例。 http://blog.tonysneed.com/2014/05/28/real-world-mvvm-with-entity-framework-and-asp-net-web-api/
http://blog.tonysneed.com/2014/05/28/real-world-mvvm-with-entity-framework-and-asp-net-web-api/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.