I'm using Entity Framework 4.1 Code First. Is there a built-in way to get a list of what properties have changed since the entity was loaded from the database? I know code first detects that an object was changed, but is there a way to get exactly what properties have changed?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
For scalar and complex properties you can use the following to extract the changed property names of an entity
myEntity
:A few things to note here:
CurrentValues.PropertyNames
only contains scalar and complex properties, not navigation properties.Complex properties means: Only the name of the complex property which is declared on the entity, not the actual individual properties of the complex type itself, for example: If you have this model...
... then, if
myEntity
is aPerson
,CurrentValues.PropertyNames
would contain "Id", "Name" and "Address" but not "Address.Country" or "Address.City" (nor "Country" or "City").If a complex property is marked as modified (
.IsModified
in the code above istrue
) then this means that either the reference (Person.Address
in the example above) has changed, no matter if actually the property values (Country
andCity
) inside of the complex type have changed or not. Or that any of the properties of the complex type has changed (Country
orCity
has changed). I believe it's not possible to find out which one, because EF always sends an UPDATE command for all complex type properties to the database, even if only one property has changed and the other remained unchanged. I would conclude from this that EF doesn't track changes of individual complex type properties.