一: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. Nim游戏变种——取纽扣谁先取完

    (2017腾讯实习生校招笔试题)Calvin和David正在玩取纽扣游戏,桌上一共有16个纽扣,两人轮流来取纽扣,每人每次可以选择取1个或3个或6个(不允许不取),谁取完最后的纽扣谁赢.Cavin和D ...

  2. 读《分布式一致性原理》zookeeper运维

    1.配置详解 1.1基本配置 基本参数包括clientPort,dataDir和tickTime 1.2高级配置 下面我们再来看看zookeeper中一些高级配置参数的配置实用 2.四字命令 我们曾经 ...

  3. GeoServer之styles定制

    GeoServer之styles定制 GeoServer中styles类似于css,将地图中的点线面画出一层皮肤,引用在图层上.不同的地方在于.GeoServer中的styles用的是sld语法,也就 ...

  4. winform中读取App.config中数据连接字符串

    1.首先要在工程引用中导入System.Configuration.dll文件的引用. 2.通过System.Configuration.ConfigurationManager.Connection ...

  5. dart 命名规范

    1.类型 首字母大写 譬如 abstract class Shape 2.变量 驼峰式命名,首字母小写 class Article { String headUrl; String user; Str ...

  6. Spring Boot tomcat参数

    主题 初学SpringBoot,想要配置一下tomcat的端口,以前tomcat直接在它的XML里配置就好了.现在SpringBoot直接继承了,不知道哪里配置.后来找到解决方法,记录一下. 具体方法 ...

  7. Python的常见异常处理

    一.异常处理 1.异常的概念 异常是错误发生的信号,一旦程序出错,并且程序没有处理这个错误,那个就会抛出异常,并且程序的运行随即终止. 2.错误种类 分两种,第一种是:语法错误,这种错误,根本过不了p ...

  8. Linux实战教学笔记53:开源虚拟化KVM(一)搭建部署与概述

    一,KVM概述 1.1 虚拟化概述 在计算机技术中,虚拟化意味着创建设备或资源的虚拟版本,如服务器,存储设备,网络或者操作系统等等 [x] 虚拟化技术分类: 系统虚拟化(我们主要讨论的反向) 存储虚拟 ...

  9. java 蓝桥杯基础练习 01字串 进制转换

    问题描述 对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能.它们的前几个是: 请按从小到大的顺序输出这32种01串. 输入格式 本试题没有输入. 输出格式 输出32行,按从小到大的顺 ...

  10. Nginx 源码完全注释(11)ngx_spinlock

    Nginx 是多进程模式的,一个 master 与多个 workers,一般工作在多核 CPU 上,所以自旋锁就是必须用到的.Nginx 中的自旋锁的定义,位于 ngx_spinlock.c 中,如下 ...