从源代码分析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的变化。。。。
转载于:https://www.cnblogs.com/dragon-L/p/6517342.html
从源代码分析DbSet如何通过ObjectStateManager管理entity lifecycle的生命周期相关推荐
- 产品生命周期管理——高效的全生命周期的产品管理方法
产品生命周期管理--高效的全生命周期的产品管理方法 & 课程背景 无论是研发.生产拟或贸易型的企业,管理的核心都是围绕如何在产品的交换(销售)环节得到更多的附加值,企业效益直接由公司产品在市 ...
- (转)Spring管理的Bean的生命周期
http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...
- 关于FragmentManager动态管理Fragment时Fragment生命周期的探究
Fragment是Android中的重要组件,在Android 3.0的时候添加进来. 关于Fragment的生命周期,我相信了解过的开发人员都应该把以下方法脱口而出:onAttach, onCrea ...
- 如何一站式管理固定资产的全生命周期?
目前很多企业还在沿用之前传统的Excel表格来管理和盘点固定资产,不但加大了企业固定资产管理员的工作量,而且在实际的操作过程中容易出现错登记.漏盘点.无法查询操作履历等各种小插曲.而且随着企业固定资产 ...
- 供应商管理系统方案,一站式管理供应商信息、生命周期、绩效和风险
想象一下,如果你全面了解新的.可靠的供应商信息,以及采购支出.绩效和风险,那么管理你的供应商将是多么容易.如果将供应商资格认证和细分工作流程纳入采购流程,那么降低风险和防止浪费又将是多么容易. 这是一 ...
- Spring管理的对象的生命周期
注:完整代码在文章最后 生命周期:就是某个对象从创建到最终销毁整个过程当中会经历的历程! 通常,需要讨论生命周期时,对应的数据类型的对象都不是由开发人员自行维护的! 被容器维护的对象,都是由容器创建对 ...
- 物流行业供应商协同管理平台供应商全生命周期管理,优化物流成本
物流行业的供应商管理一直是个难题:人员流动性大,供应商资源无法有效沉淀.寻源过程不可追溯.全生命周期缺乏管理,导致供应商资源流失严重.绩效难以有效衡量,影响主营业务的健康可持续发展. 物流公司对于物流 ...
- 谈AK管理之基础篇 - 如何进行访问密钥的全生命周期管理?
简介: 我们也常有听说例如AK被外部攻击者恶意获取,或者员工无心从github泄露的案例,最终导致安全事故或生产事故的发生.AK的应用场景极为广泛,因此做好AK的管理和治理就尤为重要了.本文将通过两种 ...
- 基于全生命周期的主数据管理:MDM详解与实战学习02 第二章 主数据管理的内涵
第二章 主数据管理的内涵 导读 2.1 主数据的概念 2.1.1 主数据的定义 2.1.2 主数据的特征 2.1.3 主数据的范围 1.描述实体范围 2.应用层次范围 (1)元数据 (2)引用数据 ( ...
最新文章
- php laravel框架 rce分析 cve-2018-15133
- Ghost 系统的过程
- java netty socket_Java-彻底弄懂netty-原来netty是这样启动的-知识铺
- Python网络编程之TCP服务器客户端(二)
- java的修饰符_java默认的修饰符是什么
- 在react中使用定时器
- 【华为云技术分享】#华为云·寻找黑马程序员#海量数据的分页怎么破?
- 【考研】考研5大分数线——国家线、院校线、自划线、单科线、录取线的区别
- 两万字整理Fabric(超级账本) 配置文件 掌握了它就掌握了Fabric的核心
- 编程基本功:代码都写不好,还写什么注释
- linux下mysql将表导出_linux下mysql导出数据表命令
- 【Python笔记】剖析Python的切片(slicing)语法
- 如何下载微信公众号中的音频、视频文件?
- 计算机考研院校录取人数,2010年计算机考研34所自主划线院校复试成绩及录取人数等详细情况汇总...
- Python基础-编码
- Xilinx Alveo加速卡开发入门
- 淘宝帝国是如何创建的连载01
- python 克里金空间插值_Python克里金(Kriging)插值计算及可视化绘制
- 个人目标日历定制制作小工具微信小程序(含源码)
- matlab图形绘制经典案例,Matlab图形绘制经典案例(转载)
热门文章
- ROS示例----TIAGo教程
- LinuxGit Accumulation
- 【初阶与进阶C++详解】第十八篇:map_set(map_set使用+multiset_multimap使用+模拟map_set)
- ElasticSearch SocketTimeoutException解决
- 原来,我连一个URL都写不对…
- MMX和SSE的运用
- 【TP5】无极限分类(树状型)
- trim函数 html,jQuery trim()函数怎么用?
- python查询ip归属地_基于Python的免费IP地址归属地查询
- 【转】桌面背景文件夹-win7文件夹背景,文件夹背景