从源代码分析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 栈 ...
随机推荐
- ftplib模块编写简单的ftp服务
from ftplib import * import os,readline import sys class MyFtp: ftp = FTP() #建立一个ftp对象的链接 '): #构造函数初 ...
- Tkinter Message
Python GUI - Tkinter Message(消息):这个小工具提供了一个多和不可编辑的对象,显示文本,自动断行和其内容的理由. 这个小工具提供了一个多和不可编辑的对象,显示文本,自动 ...
- [Z] 关于Python Tornado的一些资料
一个简单的样例: http://osedu.net/article/python/2014-03-18/501.html ioloop的官方doc: http://www.tornadoweb.org ...
- Directshow 判断音视频设备是否被占用<转>
直接上代码吧: 代码是参考网上大神分享的,在原基础上做了些修改(只检测视频设备): int DeviceIsBusy(char *videoName) { //输入设备的音视频名称 HRESULT h ...
- Jquery+Ajax实现Select动态添加数据
https://blog.csdn.net/zhengxiangwen/article/details/46480687 最近在工作中,遇到了一个关于select的问题.一般情况下,select下拉框 ...
- Docker CE部署
一.概述 Docker 在1.13版本之后,从2017年的3月1日开始,版本命名规则变为如下: 项目 说明 版本格式 YY.MM Stable 每个季度发行 Edge版本 每个月发行 同时Docker ...
- ajax+servlet 简易时间效果
<!DOCTYPE html> <html> <head> <title>index.html</title> <meta name= ...
- LINK : fatal error LNK1104: cannot open file "mfc42d.lib"
VC++6.0上建立了个基于MFC应用程序,在编译时候没出现错误,但在LINK的是时候出现这样的错误:Linking...LINK : fatal error LNK1104: cannot open ...
- Tsung压力测试:Openfire
环境准备 安装Tsung.安装openfire.安装Spark 要对openfire进行压力测试,因此我们主要讲解如何利用jabber_register.xml在openfire上面注册用户,以及利用 ...
- Android代码速查,写给新手的朋友们[转]
原文地址:http://www.open-open.com/lib/view/open1397286499090.html 0 android 创建按钮 Button button = new But ...