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:

  1. Using PrimaryKey (Id) property of an entity
  2. 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.

最新文章

  1. linux常见命令总结
  2. Centos 7防火墙firewalld开放80端口(转)
  3. C# 泛型的协变和逆变
  4. 【iCore3 双核心板_ uC/OS-III】例程八:互斥信号量
  5. [Hibernate] - EAGER and LAZY
  6. Android(java)学习笔记62:继承Thread类创建线程类
  7. ArcMap10.1无法保存编辑的内容
  8. Recovery和Charger模式下屏幕旋转180度[转]
  9. 联系我们_站内信息_站内资讯_网上定制衬衫|衬衫定制|衬衫定做-ChenShanLe衬衫乐
  10. 给div中动态添加节点并设置样式
  11. Android核心基础(十一)
  12. Lua基础(一)
  13. ORA-01994: GRANT failed: password file missing or disabled
  14. POJ 1113 Wall (凸包)
  15. 什么是IPFS?(二)
  16. Vue接口异常时处理
  17. js的七大设计原则--迪米特原则
  18. 惊喜的gift
  19. vi中批量加注释
  20. QT开发环境搭建

热门文章

  1. SFTP服务器使用指南(1)——安装搭建freeSSHd
  2. python lambda 用途
  3. bzoj 4710 分特产
  4. Linux命令学习(22):ss命令
  5. C#程序性能优化
  6. controller返回js中文变成?解决方案
  7. poj 2096 , zoj 3329 , hdu 4035 —— 期望DP
  8. navicate笔记
  9. QtCreator开启-O编译优化的方式
  10. 映射块设备提示rbd: sysfs write failed的解决方法