C# 检测实体框架对象是否已更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19221141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect if an entity framework object has changed
提问by DermFrench
I want to check if an entity object has changed (including if any of its children have been modified). I'm using Entity Framework 4.4
我想检查一个实体对象是否已更改(包括它的任何子对象是否已被修改)。我正在使用实体框架 4.4
My code basically loads in the object from the DB and applies values to some properties (however they may be the same as the original).
我的代码基本上从数据库加载对象并将值应用于某些属性(但它们可能与原始属性相同)。
I know I can check entityObject.EntityState
, but does it update if children have a change, or do I need to loop through all the children in the object?
我知道我可以检查entityObject.EntityState
,但是如果孩子有变化,它会更新,还是我需要遍历对象中的所有孩子?
This is because if it has I need to run a whole bunch of other code (like send an email alert to a user and stuff saying that values have been updated, but I don't want to run that code if they haven't been updated).
这是因为如果它有我需要运行一大堆其他代码(例如向用户发送电子邮件警报和说值已更新的内容,但如果它们尚未更新,我不想运行该代码更新)。
回答by Alexander Taran
Take a look at this question: Entity Framework 5 - DbContext Has Changes?
看看这个问题: Entity Framework 5 - DbContext Has Changes?
if you use a dbContext for only this operation you might have your solution.
如果您仅将 dbContext 用于此操作,则可能有您的解决方案。