What is the Unchanged state entity key value

After read MSDN Attaching and detaching object.

Objects are attached to the object context in an Unchanged state . if we called the Attach method. And In the Unchanged state, the Entity Framework treats the entity key values as final.

What is the "final" means?


An object in the "Unchanged" state means that the context is unaware of any changes to the entity compared to the state in the database.

You could artificially get to this state in error (for example) by doing this:

  • Loading an entity from the database using one context instance.
  • Detach it from that first context instance.
  • Change the properties.
  • Add the entity to a new context instance.
  • The new context instance will be unaware of changes to the entity with respect to data in the database, and will treat all property values as the final state of the object.

    Specifically with regard to the values of entity keys, the assumption is that the keys represent the correct value as currently in the database, so the keys will not be modified / fixed up.


    The Key value is the unique identifier for the entity and once it is attached it cannot be altered. All other properties of the entity can be changed after it has been attached.

    链接地址: http://www.djcxy.com/p/33546.html

    上一篇: 如何从Entity Framework的Auditlog中获取实体的ID 6

    下一篇: 什么是Unchanged状态实体键值