关于 Entity Framework 状态,特别是在使用 SaveChanges
调用更新记录时,我知道它只会更新已修改 EntityState
的记录(忽略添加/暂时删除)
我的问题是如何跟踪它以及它如何处理值未更改的分配?
我正在尝试计算是否
if (value1.Text != table.value1) { table.value1 = value1.Text; }
有必要停止多余的更新,或者如果我可以逃脱:
table.value1 = value1.Text;
或者是否会将状态设置为记录上的“已修改”标志,即使值相同?
谢谢!
最佳答案
如 https://msdn.microsoft.com/en-us/data/jj556205 所示,
When using most POCO entities the determination of how an entity has changed (and therefore which updates need to be sent to the database) is handled by the Detect Changes algorithm. Detect Changes works by detecting the differences between the current property values of the entity and the original property values that are stored in a snapshot when the entity was queried or attached.
调用 DbContext.SaveChanges 时, Entity Framework 会自动运行上述检测更改。而EF实际上是通过比较当前值和原始值来判断变化的
关于c# - Entity Framework EntityState 和 SaveChanges 行为无需更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31357584/