从源代码分析DbSet如何通过ObjectStateManager管理entity lifecycle的生命周期
一: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的生命周期的更多相关文章
- hostapd源代码分析(三):管理帧的收发和处理
hostapd源代码分析(三):管理帧的收发和处理 原文链接:http://blog.csdn.net/qq_21949217/article/details/46004379 这篇文章我来讲解一下h ...
- 关于FragmentManager动态管理Fragment时Fragment生命周期的探究
Fragment是Android中的重要组件,在Android 3.0的时候添加进来. 关于Fragment的生命周期,我相信了解过的开发人员都应该把以下方法脱口而出:onAttach, onCrea ...
- (转)Spring管理的Bean的生命周期
http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...
- Linux内存管理 (14)匿名页面生命周期
专题:Linux内存管理专题 关键词:匿名页面.换入.换出. 如果要将匿名页面的生命周期进行划分,大概就是诞生.使用.换出.换入和销毁. 内核中使用匿名页面的地方有很多,产生缺页中断之后匿名页面就诞生 ...
- Android 中Activity生命周期分析:Android中横竖屏切换时的生命周期过程
最近在面试Android,今天出了一个这样的题目,即如题: 我当时以为生命周期是这样的: onCreate --> onStart -- ---> onResume ---> onP ...
- activity生命周期分析(两个activity之间跳转的生命周期执行顺序)
NoteMainActivity点击跳转至NoteListActivity 我们都了解: 当A界面点击进入B界面时,此时 A===onPause--->onStop ...
- cocos2d内存管理,类的生命周期
下面资料来自<Cocos2d-x之Lua核心编程>
- SkylineGlobe 如何实现工程进度管理或者说是对象生命周期管理
SkylineGlobe 的 TerraExplorer Pro里面,给我们提供了一个Timespan Tags工具,通过这个工具,我们可以设置ProjectTree任务组对象的生命周期: 然后通过调 ...
- JVM的内存管理、对象的生命周期、内存泄漏
1 JVM内存 分为“堆”.“栈”和“方法区”三个区域,分别用于存储不同的数据 1.1 堆 JVM在其内存空间开辟一个称为”堆”的存储空间,这部分空间用于存储使用new关键字所创建的对象. 1.2 栈 ...
随机推荐
- C经典实例
1,九九乘法表 undefined reference to `sqrt' linux本身无math库 -lm,伪链接;
- Vue使用echarts
Vue使用echarts 该示例使用 vue-cli 脚手架搭建 安装echarts依赖 npm install echarts -S 创建图表 全局引入 main.js // 引入echarts i ...
- 转载----我与CMDB不得不说的故事
每次读到配置管理相关的书籍时,我总在想:“这些定义很精准,流程也很完整,但这不是真正的难题.”对于一个配置管理者来说,真正的难题不是绘制“庞大而精美”的数据模型,不是设计“全天候.无死角”的管控流程, ...
- nginx 里设置font-face 跨域
server { ... # Fix @font-face cross-domain restriction in Firefox location ~* \.(eot|ttf|woff)$ { ad ...
- css常用属性初总结第一弹:id和class选择器
前言:这是我第二次写博客,记录自己所学的点点滴滴,希望大家一起共勉! 说到选择器大家都不陌生,估计用的最多的还是id选择器和类选择器,至于其他的选择器,今天在这里我避而不谈. 类选择器:将html元素 ...
- 多视几何——三角化求解3D空间点坐标
VINS-Mono / VINS-Fusion中triangulatePoint()函数通过三角化求解空间点坐标,代码所体现的数学描述不是很直观,查找资料,发现参考文献[1]对这个问题进行详细解释,记 ...
- Python3 获取RDS slowlog+微信接口报警
一.功能说明 二.代码详情 1.通过阿里sdk获取慢查询列表,格式化. 2.企业微信报警接口 3.deamon #!/usr/bin/python #-*- conding:utf-8 -*- fro ...
- sqlserver三种数据集合运算
2.1 并集运算(UNION) (1)UNION ALL(不删除重复行) Code: 1 SELECT empID,empName,position,degree 2 FROM Employees ...
- springboot+beetlsql+mysql整合
一.工程目录结构 二.pom.xml文件配置 <dependency> <groupId>mysql</groupId> <artifactId>mys ...
- 基于weui的一个小插件
移动端项目当中大量的使用了weui,为了减少工作量,方便修改,自己写了个小插件,暂时只有toast和dialog部分,可能会更新actionSheet等其他部分 更新一个手机端预览的二维码,就直接放项 ...