Entity Framework Tutorial Basics(27):Update Entity Graph
Update Entity Graph using DbContext:
Updating an entity graph in disconnected scenario is a complex task. It is easy to add a new entity graph in disconnected mode, but to update an entity graph needs careful design consideration.
The problem in updating an entity graph in the disconnected scenario, is that the context doesn't know what operation was performed on it at the client side. As per the following figure, the new context doesn't know the state of each entity:
We need to identify the states of each entity in the entity graph, before calling the SaveChages() method of context. There are different patterns to identify entity state, which we need to consider in designing data layer with Entity Framework.
Patterns of Identifying Entity State in disconnected scenario:
There are several ways (some shown below) to identify an entity state in disconnected scenario:
- Using PrimaryKey (Id) property of an entity
- Having state property in entity set
Note: You can create your own design to identify an entity state based on your architecture. This is just for learning purposes.
1) Using PrimaryKey property of an entity:
You can use the PrimaryKey (Id) property of each entity to determine its entity state. However, you have to decide which of the following architectural rule to use:
- Each type of entity must have Id property (PK)
- Default value of Id property should be 0
As you can see in the figure shown above, the client fetches the Standard and Teacher entity graph and does some operations on it and then sends it to Context 2 to save the changes.
In the disconnected scenario, context2 doesn't know the state of each entity. It has to determine the state of the Standard entity by using StandardId and the state of the Teacher entity by using TeacherId property. If StandardId and TeacherID value is zero, that means it is a new entity and if it is not zero then it is a modified entity.
In the above figure, Standard, Teacher 1, and Teacher 2 have a non-zero Id property value so they are marked as Modified and Teacher 3 Id is zero so it is marked as Added.
The code snippet shown below uses Id property to mark an entity state:
Standard disconnectedStandard = null; using (var context = new SchoolDBEntities())
{
context.Configuration.ProxyCreationEnabled = false; disconnectedStandard = context.Standards.Where(s => s.StandardId == ).Include(s => s.Teachers).FirstOrDefault<Standard>();
}
//Update Standard in disconnected mode
disconnectedStandard.StandardName = "Edited Standard Name"; //Update teachers collection by editing first teacher and adding new teacher
disconnectedStandard.Teachers.ElementAt().TeacherName = "Edited Teacher Name";
disconnectedStandard.Teachers.Add(new Teacher() { TeacherName = "New Teacher", StandardId = disconnectedStandard.StandardId }); using (var newContext = new SchoolDBEntities())
{
//mark standard based on StandardId
newContext.Entry(disconnectedStandard).State = disconnectedStandard.StandardId == ? EntityState.Added : EntityState.Modified; //mark teacher based on StandardId
foreach (Teacher tchr in disconnectedStandard.Teachers)
newContext.Entry(tchr).State = tchr.TeacherId == ? EntityState.Added : EntityState.Modified; newContext.SaveChanges();
}
Advantages:
- No need for extra coding/processing to determine entity state.
- Good performance.
Disadvantages:
- Every entity type needs to have an Id property. It cannot determine states of an entity that do not have Id property.
- Cannot identify an Unchanged entity. It is set to Modified state even if the entity is not changed at all. So, there is an unnecessary database update statement for an unchanged entity.
- Cannot handle delete entity scenario. It needs to handle deletion separately.
2) Having State property in every entity:
Another way to determine entity state is to have a State property in every entity type and set an appropriate state from the client side in disconnected mode. Then we just need to set the entity state as the state property of an entity object before calling SaveChanges of the new context.
Example:
First of all, create an IEntityState interface with enum property called ObjectState:
interface IEntityObjectState
{
EntityObjectState ObjectState { get; set; }
} public enum EntityObjectState
{
Added,
Modified,
Deleted,
Unchanged
}
Now, implement an IEntityObjectState in each entity class, e.g. Standard and Teacher entity, as shown below:
public partial class Standard:IEntityObjectState
{
public Standard()
{
this.Students = new HashSet<Student>();
this.Teachers = new HashSet<Teacher>();
} public int StandardId { get; set; }
public string StandardName { get; set; }
public string Description { get; set; } public virtual ICollection<Student> Students { get; set; }
public virtual ICollection<Teacher> Teachers { get; set; }
[NotMapped]
public EntityObjectState ObjectState
{
get;
set;
}
} public partial class Teacher:IEntityObjectState
{
public Teacher()
{
this.Courses = new HashSet<Course>();
} public int TeacherId { get; set; }
public string TeacherName { get; set; }
public Nullable<int> StandardId { get; set; } public virtual ICollection<Course> Courses { get; set; }
public virtual Standard Standard { get; set; } [NotMapped]
public EntityObjectState ObjectState
{
get;
set;
} }
Set the appropriate state in disconnected mode on client side:
Teacher existingTeacher = null; using (var context = new SchoolDBEntities())
{
context.Configuration.ProxyCreationEnabled = false;
existingTeacher = context.Teachers.FirstOrDefault<Teacher>(); }
Standard disconnectedStandard = new Standard() { StandardName = "New Standard", ObjectState = EntityObjectState.Added };
existingTeacher.ObjectState = EntityObjectState.Modified;
//add existing teacher(in db) to standard
disconnectedStandard.Teachers.Add(existingTeacher);
//add new standard
disconnectedStandard.Teachers.Add(new Teacher() { TeacherName = "New teacher", StandardId = disconnectedStandard.StandardId, ObjectState = EntityObjectState.Added });
Set the entity state as per ObjectState before calling SaveChanges.
using (var newContext = new SchoolDBEntities())
{
//check the ObjectState property and mark appropriate EntityState
if (disconnectedStandard.ObjectState == EntityObjectState.Added)
newContext.Entry(disconnectedStandard).State = System.Data.Entity.EntityState.Added;
else if (disconnectedStandard.ObjectState == EntityObjectState.Modified)
newContext.Entry(disconnectedStandard).State =System.Data.Entity.EntityState.Modified;
else if (disconnectedStandard.ObjectState == EntityObjectState.Deleted)
newContext.Entry(disconnectedStandard).State = System.Data.Entity.EntityState.Deleted;
else
newContext.Entry(disconnectedStandard).State = System.Data.Entity.EntityState.Unchanged; //check the ObjectState property of each teacher and mark appropriate EntityState
foreach (Teacher tchr in disconnectedStandard.Teachers)
{
if (tchr.ObjectState == EntityObjectState.Added)
newContext.Entry(tchr).State = System.Data.Entity.EntityState.Added;
else if (tchr.ObjectState == EntityObjectState.Modified)
newContext.Entry(tchr).State = System.Data.Entity.EntityState.Modified;
else if (tchr.ObjectState == EntityObjectState.Deleted)
newContext.Entry(tchr).State = System.Data.Entity.EntityState.Deleted;
else
newContext.Entry(tchr).State = System.Data.Entity.EntityState.Unchanged;
}
//save changes
newContext.SaveChanges();
}
Advantages:
- No need for extra coding/processing to determine the entity state
- Handles Added, Modified, Deleted, and Unchanged states properly
- No unnecessary update call for unchanged entities.
Disadvantage:
- Need to set the appropriate states of each entity in disconnected mode. So there is a need to be extra careful in disconnected mode.
Entity Framework Tutorial Basics(27):Update Entity Graph的更多相关文章
- Entity Framework Tutorial Basics(4):Setup Entity Framework Environment
Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...
- Entity Framework Tutorial Basics(42):Colored Entity
Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that ...
- Entity Framework Tutorial Basics(5):Create Entity Data Model
Create Entity Data Model: Here, we are going to create an Entity Data Model (EDM) for SchoolDB datab ...
- Entity Framework Tutorial Basics(26):Add Entity Graph
Add Entity Graph using DbContext: Adding entity graph with all new entities is a simple task. We can ...
- Entity Framework Tutorial Basics(24):Update Single Entity
Update Existing Entity using DBContext in Disconnected Scenario: In this chapter, you will learn how ...
- Entity Framework Tutorial Basics(40):Validate Entity
Validate Entity You can write custom server side validation for any entity. To accomplish this, over ...
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- Entity Framework Tutorial Basics(32):Enum Support
Enum in Entity Framework: You can now have an Enum in Entity Framework 5.0 onwards. EF 5 should targ ...
- Entity Framework Tutorial Basics(31):Migration from EF 4.X
Migration from Entity Framework 4.1/4.3 to Entity Framework 5.0/6.0 To migrate your existing Entity ...
随机推荐
- 在Windows 7上安装ACE 6.1.0
主机环境 操作系统:Windows 7 专业版准备ACE 用浏览器打开http://download.dre.vanderbilt.edu/,下载ACE-6.1.0和ACE-html-6. ...
- Tomcat的安装与环境配置
首先,下载地址为:http://tomcat.apache.org/,在左侧的列表中找到Download,找到自己要下载的Tomcat的版本.我下载的是Tomcat 7. 进入后出现如上右图界面.我选 ...
- Git 版本管理工具(一)
转自:http://blog.csdn.net/ithomer/article/details/7527877 Git 是一个分布式版本控制工具,它的作者 Linus Torvalds 是这样给我们介 ...
- qduoj su003 数组合并
描述 现在呢有两个整形数组,a[n]和b[m],而且已经知道这两个数组都是非降序数组.现在呢就有一个工作需要你来完成啦.对于a中的每个元素a[i]在b中寻找<=a[i] 的元素个数,个数记为x[ ...
- CH5E02 [IOI1999]花店橱窗[暴力dp]
众所周知,这个人太菜了,所以她又来切水题了. 显然设计状态表示第$i$朵花放第$j$瓶中的最大价值.然后瞎转移一波是n三方的,加个前缀max变成n方就水过去了. 当然这题可以搜索剪枝的. 虐lyd书上 ...
- 使用Snapdragon Profiler工具分析
http://blog.csdn.net/cgx090902/article/details/73849202 Snapdragon Profiler(骁龙分析器)是一款性能分析软件,在Windows ...
- [转]Angular移除不必要的$watch之性能优化
双向绑定是Angular的核心概念之一,它给我们带来了思维方式的转变:不再是DOM驱动,而是以Model为核心,在View中写上声明式标签.然后,Angular就会在后台默默的同步View的变化到Mo ...
- Operating System-进程/线程内部通信-信号量、PV操作的实现和应用(解决哲学家进餐和生产者消费者问题)
本文主要内容: 信号量的实现 利用信号量解决哲学家用餐问题 利用信号量解决生产者消费者问题 一.信号量的实现 1.1 信号量结构 typedef struct { int value; struct ...
- docker registry 镜像删除
registry:2.5.0版本的镜像,将镜像默认存放在了/var/lib/registry 目录下 /var/lib/registry/docker/registry/v2/repositories ...
- ping错误详解
在网络中Ping 是一个十分好用的TCP/IP工具,它主要的功能是用来检测网络的连通情况和分析网络速度. 输入 ping /? 例出ping的参数 使用Ping检查连通性有五个步骤 1. 使用ipco ...