一: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. Python小知识点(2)

    1.简述Python的运行过程 在说这个问题之前,我们先来说两个概念,PyCodeObject和pyc文件. 我们在硬盘上看到的pyc自然不必多说,而其实PyCodeObject则是Python编译器 ...

  2. LR11直接对数据库访问操作方法在性能测试中的应用总结

    项目背景概述 某测试项目,该项目的接口测试需要大量的订单,并且需要订单的状态是已确认客户的订单,大量的订单可以通过下单接口直接造订单数据,但下的订单要人工在后台页面处理到已确认客户的状态才可以使用这些 ...

  3. 2、keys相关命令

    redis的官网http://redis.io是学习redis的重要资源库,所有命令都分门别类的罗列在了这里http://redis.io/commands. 1.数据库选择命令: SELECT in ...

  4. leetcode840

    本题不清楚题意,从网上找到了python的解答,记录如下. class Solution: def numMagicSquaresInside(self, grid): ans, lrc = 0, [ ...

  5. Spring Boot实践——三种拦截器的创建

    引用:https://blog.csdn.net/hongxingxiaonan/article/details/48090075 Spring中的拦截器 在web开发中,拦截器是经常用到的功能.它可 ...

  6. SVN用命令行更换本地副本IP地址

    1.运行svn info现连的svn信息 2.运行svn switch --relocate https://原来的地址 https://改过后的地址. svn help switch 查看switc ...

  7. .net core 下的Area注册

    app.UseMvc(routes => { routes.MapAreaRoute( name: "AreaRoute", areaName: "Admin&qu ...

  8. Java Tutorial

    http://www.tutorialspoint.com/java/index.htm

  9. ShadowVolume

    [ShadowVolume] 1.z-pass 算法. z-pass 是 shadow volume 一开始的标准算法,用来确定某一个象素是否处于阴影当中.其原理是: Pass1:enable z-b ...

  10. 网站转为https协议,苹果商店应用转为https协议总结

    log_format www.44755.com '$remote_addr - $remote_user [$time_local] "$request" ' '$status ...