更新对象图,当实体框架断开的行为 [英] Disconnected Behavior of Entity Framework when Updating Object Graph

查看:175
本文介绍了更新对象图,当实体框架断开的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与使用以下技术的项目。

I'm currently working with a project that use following technologies.


  1. ASP.net MVC - presentation层
  2. 数据服务层 - (WCF)

  3. 数据传输对象(DTO)与自动映射层

  4. 域层(POCO,code首先实体框架)

  5. 库层+实体框架4.3 +的DbContext。

我们使用的DTO域对象反之亦然使用自动映射器和发送到前端转换使用WCF服务。

We use DTOs to convert Domain objects vice versa using auto mapper and sent to front end by using WCF Service.

此外,我们正在创建基于每个请求的DbContext在WCF层为每个请求和我们的WCF服务上下文由每呼叫并没有跟踪构造能够在客户端的DTO,这是完全断开。

Also, we are creating per request based DBContext in WCF layer for each request and our WCF service context is constructed by Per Call and No Tracking enable in client side DTOs and it is fully disconnected.

此外,我们还对象图如下。

Also we have following object graph.

public class User : BaseEntity
    {
        public virtual Identity Identity { get; set; }

        public string UserName { get; set; }

        public string Password { get; set; }

        public int IdentityId { get; set; }

        public virtual IList<Group> Groups{ get; set; }
    }



 public class Identity : BaseEntity
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public virtual IList<Email> Emails { get; set; }

        public virtual IList<PhoneNumber> PhoneNumbers { get; set; }
    }

相比,我们的域名结构DTO更像是一样的。

Our Dto structure is more like same as compared to Domain.

我的问题:

当涉及到更新的对象图,例如: UpdateUser两个(用户用户);什么是实体框架的最佳方法?

When it comes to update object graph For Example: UpdateUser(User user); what is the best approach with Entity Framework ?

现在我们用单一的功能,节省导航数据例如: UpdateEmail(用户ID,邮箱)(只保存原始数据没有关系);所以这让很多在数据库中插入和更新的时候,我们考虑之一的UnitOfWork的。

Now we use single functions to save Navigation data ex:UpdateEmail(userId, Email)(only saves primitive data not relationships); so it makes a lot of inserts and updates in the data base when we considering the one UnitOfWork.

当前实现如下:

  public void UpdateUser(User user)
    {
    UpdateEmail(user.userId, user.Idenity.Emails);
    UpdatePhone(user.userId, user.Identity.PhoneNumbers);

    etc.............

    UpdateUser(user);
    UnitOfWork.Commit();// Calling DbContext.SaveChanges();
    }

有没有办法,我们就可以在上述情况与实体框架使用与断开连接的对象图中的任何图案或最佳实践?

Is there any pattern or best practice that we can use with Entity Framework in above situation with disconnected object graph ?

推荐答案

有没有太多的选择来解决它。 <一href=\"http://stackoverflow.com/questions/3635071/update-relationships-when-saving-changes-of-ef4-poco-objects/3635326#3635326\">EF没有更新断开连接的对象图的任何直接suppor 吨。您必须code您的更新逻辑,一般有两种方法如何做到这一点:

There are not many option to solve it. EF doesn't have any direct support for updating disconnected object graphs. You must code your update logic and there are generally two ways how to do it:


  • 当您从服务请求接收更新的用户,您将调用数据库,并获取当前数据库状态=用户与所有受影响的关系。您将使用的数据库版本和更新版本,以打造在年底EF将更新设置,以便有效的变化,插入和删除,只对已经真正改变的数据。

  • 您将修改您的DTO的运输状态,以及和你的客户将负责设定他做的DTO修改的类型。您将使用这些信息来正确地配置 ChangeTracker 每收到实体。

  • When you receive updated user from the service request you will call database and fetch current database state = user with all affected relations. You will use database version and updated version to build valid change set so at the end EF will update, insert and delete only data which have really changed.
  • You will modify your DTOs to transport state as well and your client will be responsible for setting the type of modification he did on the DTO. You will use this information to correctly configure ChangeTracker for every received entity.

这篇关于更新对象图,当实体框架断开的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆