From source code Analysis Dbset How to manage the life cycle of entity lifecycle through Objectstatemanager

Source: Internet
Author: User

A: When SaveChange, how to know which entity is Add,modify,delete,unchange????


How to discern ...

Make a mark in the entity to indicate ... have been followed ... When each entity is labeled, we can

Get the appropriate action from these tags ...


Two: How does EF do that? Objectstatemanager class to manage the markup for each entity ...

Private Dictionary<entitykey, entityentry> _addedentitystore;

Private Dictionary<entitykey, entityentry> _deletedentitystore;

Private Dictionary<entitykey, entityentry> _modifiedentitystore;

Private Dictionary<entitykey, entityentry> _unchangedentitystore;


private void Addentityentrytodictionary

Dbset.add does this by stuffing the new entity into the specified dic ...


When SaveChange is acquired:

This. Pullmodifiedentriesfromstatemanager ();

This. Pullunchangedentriesfromstatemanager ();


private void Pullmodifiedentriesfromstatemanager ()
{
foreach (System.Data.Entity.Core.IEntityStateEntry entry in This._statemanager.getentitystateentries ( entitystate.added))
{
if (!entry. Isrelationship &&!entry. Iskeyentry)
{
This. Keymanager.registerkeyvalueforaddedentity (entry);
}
}
foreach (System.Data.Entity.Core.IEntityStateEntry entry2 in This._statemanager.getentitystateentries ( entitystate.modified | entitystate.deleted | entitystate.added))
{
This. Registerreferentialconstraints (Entry2);
}
foreach (System.Data.Entity.Core.IEntityStateEntry entry3 in This._statemanager.getentitystateentries ( entitystate.modified | entitystate.deleted | entitystate.added))
{
This. Loadstateentry (Entry3);
}
}


Internal virtual ienumerable<objectstateentry> getobjectstateentriesinternal (entitystate State)
{
objectstateentry[] Entryarray = new Objectstateentry[this. Getobjectstateentriescount (state)];
int num = 0;
if ((entitystate.added & state)! = 0) && (this._addedrelationshipstore! = null))
{
foreach (Keyvaluepair<relationshipwrapper, relationshipentry> pair in This._addedrelationshipstore)
{
entryarray[num++] = pair. Value;
}
}
if ((entitystate.deleted & state)! = 0) && (this._deletedrelationshipstore! = null))
{
foreach (Keyvaluepair<relationshipwrapper, relationshipentry> pair2 in This._deletedrelationshipstore)
{
entryarray[num++] = Pair2. Value;
}
}
if ((Entitystate.unchanged & state)! = 0) && (this._unchangedrelationshipstore! = null))
{
foreach (Keyvaluepair<relationshipwrapper, relationshipentry> pair3 in This._unchangedrelationshipstore)
{
entryarray[num++] = pair3. Value;
}
}
if ((entitystate.added & state)! = 0) && (this._addedentitystore! = null))
{
foreach (Keyvaluepair<entitykey, entityentry> pair4 in This._addedentitystore)
{
entryarray[num++] = pair4. Value;
}
}
if ((Entitystate.modified & state)! = 0) && (this._modifiedentitystore! = null))
{
foreach (Keyvaluepair<entitykey, entityentry> pair5 in This._modifiedentitystore)
{
entryarray[num++] = Pair5. Value;
}
}
if ((entitystate.deleted & state)! = 0) && (this._deletedentitystore! = null))
{
foreach (Keyvaluepair<entitykey, entityentry> pair6 in This._deletedentitystore)
{
entryarray[num++] = pair6. Value;
}
}
if ((Entitystate.unchanged & state)! = 0) && (this._unchangedentitystore! = null))
{
foreach (Keyvaluepair<entitykey, entityentry> pair7 in This._unchangedentitystore)
{
entryarray[num++] = pair7. Value;
}
}
return entryarray;
}


Getentitystateentries


Three: Since we savechange is the time, is through the state of the entity to obtain ...

//
Summary:
Describes the state of an entity.
[Flags]
[SuppressMessage ("Microsoft.Naming", "Ca1714:flagsenumsshouldhavepluralnames")]
public enum EntityState
{
//
Summary:
The entity is not a being tracked by the context. An immediately
After it have been created with the new operator or with one of the System.Data.Entity.DbSet
Create methods.
Detached = 1,
//
Summary:
The entity is being tracked by the context and exists in the database, and its
The property values has not changed from the values in the database.
unchanged = 2,
//
Summary:
The entity is being tracked by the context but does not yet exist in the database.
Added = 4,
//
Summary:
The entity is being tracked by the context and exists
Been marked for deletion from the database the next time SaveChanges is called.
Deleted = 8,
//
Summary:
The entity is being tracked by the context and exists in the database, and some
or all of their property values has been modified.
Modified = 16
}

using (schooldbentities db = new Schooldbentities ())
{
var item = db. Students.firstordefault ();

Item. Studentname = "ASDFASDFASDFASD";

Db. SaveChanges ();
}

There is a comparator to determine whether it is "local modification" or "Global modification" ....


The inevitable way of warehousing mode, how to track the change of entity ....

From source code Analysis Dbset How to manage the life cycle of entity lifecycle through Objectstatemanager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.