一:Savechange的时候,怎么知道哪些entity被add,modify,delete,unchange ????

如何来辨别。。。

在entity中打上标记来做表示。。。已经被跟踪了。。。当每个entity被打上标记之后,我们才可以

从这些标记获取相应的操作。。。

二:ef如何做到的。。 ObjectStateManager类来管理每个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 做的操作 将新的entity塞入到指定的dic中。。。

SaveChange获取的时候:

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

var state=db.Entity(obj).State;

db.Entity(obj).State=EntityState.Add

三:既然我们savechange是的时候,是通过entity的状态去获取。。。

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

using (SchoolDBEntities db = new SchoolDBEntities())
{
var item = db.Students.FirstOrDefault();

item.StudentName = "asdfasdfasdfasd";

db.SaveChanges();
}

//有一个比较器,来判断是”局部修改“ 还是 ”全局修改“。。。。

仓储模式的必然之路,如何跟踪entity的变化。。。。

从源代码分析DbSet如何通过ObjectStateManager管理entity lifecycle的生命周期的更多相关文章

  1. hostapd源代码分析(三):管理帧的收发和处理

    hostapd源代码分析(三):管理帧的收发和处理 原文链接:http://blog.csdn.net/qq_21949217/article/details/46004379 这篇文章我来讲解一下h ...

  2. 关于FragmentManager动态管理Fragment时Fragment生命周期的探究

    Fragment是Android中的重要组件,在Android 3.0的时候添加进来. 关于Fragment的生命周期,我相信了解过的开发人员都应该把以下方法脱口而出:onAttach, onCrea ...

  3. (转)Spring管理的Bean的生命周期

    http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...

  4. Linux内存管理 (14)匿名页面生命周期

    专题:Linux内存管理专题 关键词:匿名页面.换入.换出. 如果要将匿名页面的生命周期进行划分,大概就是诞生.使用.换出.换入和销毁. 内核中使用匿名页面的地方有很多,产生缺页中断之后匿名页面就诞生 ...

  5. Android 中Activity生命周期分析:Android中横竖屏切换时的生命周期过程

    最近在面试Android,今天出了一个这样的题目,即如题: 我当时以为生命周期是这样的: onCreate --> onStart -- ---> onResume ---> onP ...

  6. activity生命周期分析(两个activity之间跳转的生命周期执行顺序)

    NoteMainActivity点击跳转至NoteListActivity 我们都了解: 当A界面点击进入B界面时,此时         A===onPause--->onStop       ...

  7. cocos2d内存管理,类的生命周期

    下面资料来自<Cocos2d-x之Lua核心编程>

  8. SkylineGlobe 如何实现工程进度管理或者说是对象生命周期管理

    SkylineGlobe 的 TerraExplorer Pro里面,给我们提供了一个Timespan Tags工具,通过这个工具,我们可以设置ProjectTree任务组对象的生命周期: 然后通过调 ...

  9. JVM的内存管理、对象的生命周期、内存泄漏

    1 JVM内存 分为“堆”.“栈”和“方法区”三个区域,分别用于存储不同的数据 1.1 堆 JVM在其内存空间开辟一个称为”堆”的存储空间,这部分空间用于存储使用new关键字所创建的对象. 1.2 栈 ...

随机推荐

  1. Flask之模板过滤器

    3.2 过滤器: 过滤器的本质就是函数.有时候我们不仅仅只是需要输出变量的值,我们还需要修改变量的显示,甚至格式化.运算等等,这就用到了过滤器. 过滤器的使用方式为:变量名 | 过滤器. 过滤器名写在 ...

  2. CSS 父级方法清除浮动方法

    .baseMod:after { clear: both; content: ' '; display: block; visibility: none; height: 1% } 2. overfl ...

  3. jquery 实现点击图片居住放大缩小

    该功能是基于jquery实现的,所以 第一步则是引入jquery jquery下载地址:https://jquery.com/download/ 或者使用此时调试的版本(3版本) /*! jQuery ...

  4. Halcon学习之三:有关图像通道的函数(R是三通道,B是1通道,G二通道),排列顺序BGR

    黑白摄像机会返回每个像素所对应的能量采用结果,这些结果组成了一幅单通道灰度值图像,而对于RGB彩色摄像机,它将返回每个像素所对应的三个采样结果,也就是一幅三通道图像.下面这些是与图像通道有关的函数: ...

  5. leetcode844

    class Solution { public: bool backspaceCompare(string S, string T) { stack<char> ST1; ; i < ...

  6. linux/ubuntu 端口开放

    在ubuntu下面开放端口好像主要有两种方法,一种是ubuntu自带的防火墙,一种是iptables,这里我们主要使用iptables.本文的系统版本为ubuntu14.04和ubuntu16.04 ...

  7. beego 中文教程

    https://www.kancloud.cn/hello123/beego/126087

  8. 35. Search Insert Position (Array; Divide-and-Conquer)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. java的web.xml中<url-pattern>配置[转]

    <servlet-mapping> <servlet-name>downLoadFile</servlet-name> <url-pattern>*.l ...

  10. CCScrollView练习

    MyScrollItem是CCScrollView容器内项的接口,MyScrollView主要处理添加子节点和事件的处理,MyScrollViewTestItem是对MyScrollItem实现的测试 ...