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.

Entity Framework Tutorial Basics(27):Update Entity Graph的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Entity Framework Tutorial Basics(24):Update Single Entity

    Update Existing Entity using DBContext in Disconnected Scenario: In this chapter, you will learn how ...

  6. Entity Framework Tutorial Basics(40):Validate Entity

    Validate Entity You can write custom server side validation for any entity. To accomplish this, over ...

  7. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 1.mysql优化---优化入门之MySQL的优化介绍及执行步骤

    优化到底优化什么?      优化,一直是面试最常问的一个问题.因为从优化的角度,优化的思路,完全可以看出一个人的技术积累.那么,关于系统优化,假设这么个场景,用户反映系统太卡(其实就是高并发),那么 ...

  2. BZOJ - 3123 森林 (可持久化线段树+启发式合并)

    题目链接 先把初始边建成一个森林,每棵树选一个根节点递归建可持久化线段树.当添加新边的时候,把结点数少的树暴力重构,以和它连边的那个点作为父节点继承线段树,并求出倍增数组.树的结点数可以用并查集来维护 ...

  3. idea 创建maven工程(入门)

    转:http://blog.csdn.net/qq_32588349/article/details/51461182 1. 下载Maven 官方地址:http://maven.apache.org/ ...

  4. Vue 中的 computed 和 methods

    Vue 中的 computed 和 methods 使用 computed 性能会更好. 如果你不希望缓存,可以使用 methods 属性.

  5. 解决mac下sublime中文乱码

    Mac OS X 属于 Apple 独家演绎的 Unix 分支版本,默认使用 UTF-8 编码,当使用不同开发平台的小伙伴们,共同维护一份代码的时候,尤其现在很多人都还在用 Windows 系统的时候 ...

  6. bzoj 1798 [Ahoi2009]Seq 维护序列seq ——线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1798 先乘后加,就可给加法标记乘上乘法标记. 注意可能有 *0 的操作,所以 pshd 时不 ...

  7. 首次db查询时延迟明显高于后面几次。

    1.如果排查到时db相关的问题的话,一般都是连接池的配置问题. 在配置好连接池时一定要注意对连接也进行初始化配置,否则可能出现连接池初始化了,但是连接并没有初始化,这样在第一次查询的时候可能会出现较大 ...

  8. [转载]rmmod: can't change directory to '/lib/modules': No such file or directory

    转载网址:http://blog.csdn.net/chengwen816/article/details/8781096 在我新移植的kernel(3.4.2)和yaffs2文件中,加载新编译的内核 ...

  9. DataGridview刷新异常的问题

    datsSet 绑定到dataGrieView,在刷新dataSet的数据时,常会bug:索引0没有值或索引(int)x没有值 昨天弄了一个下午,发现bug原因: dataGridView中有数据时, ...

  10. LTE流程

    LTE 过程全流程 1. UE处于关闭状态 2. 打开UE电源. 3. 搜索附近的频率 4. 同步时间 5. 小区搜索 6. 小区选择 7. 解码MIB 8. 解码SIB 9. 初始化RACH过程 1 ...