从源代码分析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 栈 ...
随机推荐
- 解决 mysql 数据库 挂掉了
问题 : mysql运行几天之后就挂掉了 , 修改了mysql 的连接数也解决不了,看代码也没有什么问题,但就是感觉哪个功能一直占着mysql资源,查了一下当前的线程状态 time的单位是 秒 , 可 ...
- C# 设计模式-工厂模式(Factory)
1.工厂模式 factory从若干个可能类创建对象. 例如:如果创建一个通信类接口,并有多种实现方式,可以使用factory创建一个实现该接口的对象,factory可以根据我们的选择,来创建适合的对象 ...
- 我的Linux之路——实现虚拟机VMware上linux与windows互相复制与粘贴
出自:http://blog.csdn.net/u012243115/article/details/40454063 解决方法:只需要在CentOS安装一个vmware-tools的工具. 1.打开 ...
- STL : List使用时应注意的问题
这篇文章所述只是本人遇到的问题,仅供参考. #include<list> #include<iostream> using namespace std; class Foo { ...
- angularjs 出现 “Possibly unhandled rejection: cancel ”错误
Try adding this to your config. I had a similar is once and this workaround did the trick. app.confi ...
- 【转】内存耗用:VSS/RSS/PSS/USS
Terms VSS- Virtual Set Size 虚拟耗用内存(包含共享库占用的内存) RSS- Resident Set Size 实际使用物理内存(包含共享库占用的内存) PSS- Prop ...
- java核心知识点----创建线程的第三种方式 Callable 和 Future CompletionService
前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...
- 657. Judge Route Circle机器人能否返回
[抄题]: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this r ...
- GitHub 上的十一款热门开源安全工具
作为开源开发领域的基石,“所有漏洞皆属浅表”已经成为一条著名的原则甚至是信条.作为广为人知的Linus定律,当讨论开源模式在安全方面的优势时,开放代码能够提高项目漏洞检测效率的理论也被IT专业人士们所 ...
- Madgwick算法详细解读
Madgwick算法详细解读 极品巧克力 前言 接上一篇文章<Google Cardboard的九轴融合算法>. Madgwick算法是另外一种九轴融合的方法,广泛应用在旋翼飞行器上,效果 ...