且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实体框架和维护实体的两个实例

更新时间:2023-02-20 11:35:03

如果对象仍连接到上下文(您尚未处理上下文),则无需从数据库中加载对象。

You don't need to load the object from the database if the object is still connected to the context (you haven't disposed of the context).

签出 ObjectStateManager.GetObjectStateEntry 。返回的对象是 ObjectStateEntry 。该对象同时具有 OriginalValues CurrentValues 属性。

Check out ObjectStateManager.GetObjectStateEntry. The object that returns is an ObjectStateEntry. That object has both OriginalValues, and CurrentValues properties.

DbContext会从对象最初加载时开始维护它的状态。这是EF在调用 context.SaveChanges()时用于确定是否需要写入数据库的方法。如果您要查找的是,则只能访问已更改的属性。这些可以在 ObjectStateEntry GetModifiedProperties()方法中找到。

The DbContext maintains the state of the object from when it was originally loaded. This is what EF uses to determine if it needs to write to the database when you call context.SaveChanges(). You can access just the properties that have changed, if that's what you're looking for. Those are found in the GetModifiedProperties() method on the ObjectStateEntry.