简体   繁体   English

避免在Entity Framework中将属性明确标记为未修改

[英]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.

相关问题 实体框架明确包含的导航属性为null - Entity Framework explicitly included navigation property is null 在实体框架中将属性标记为“在添加或更新时生成的值”有什么意义? - What is the point of marking a property as “Value generated on add or update” in Entity Framework? 如何避免在Entity Framework中加载集合属性 - How to avoid to load collection property in Entity Framework 覆盖 SaveChangesAsync Entity Framework 6 以记录修改的属性值 - Override SaveChangesAsync Entity Framework 6 to log modified property values 实体未能使用Entity框架进行修改 - Entity failed to be modified with Entity framework 在Entity Framework中明确关闭连接 - Closing connections explicitly in Entity Framework 如何在保存时自动更新Entity Framework 4中实体的Modified属性? - How to auto-update the Modified property on a Entity in Entity Framework 4 when saving? 我是否需要使用实体框架将属性 int 显式添加到组合对象的外键中? - Do I need to explicitly add a property int to foreign keys for composed objects using Entity Framework? 如何在不显式设置每个属性的情况下加载Entity Framework数据对象 - How to load an Entity Framework data object without explicitly setting each property 将集合设置为已修改的实体框架 - Set collection to modified Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM