Disconnected Behavior of Entity Framework when Updating Object Graph

橙三吉。 提交于 2019-12-02 05:12:06

问题


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

  1. ASP.net MVC - presentation Layer
  2. Data Service Layer - (WCF)
  3. Data Transfer Objects(DTO) Layer with Auto Mapper
  4. Domain Layer (POCO, Code First Entity Framework)
  5. Repository Layer + Entity Framework 4.3 + DbContext.

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

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; }
    }

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

My Questions:

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

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.

Current implementation as follows

  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 ?


回答1:


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:

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



回答2:


Julie Lerman explains this very well in some video's




回答3:


Is your issue now that you use EF and it makes lots of calls for updates? I don't see a way around that here as your phone numbers and emails are separate tables. You could create a flattened entity for the user that contains ex five of each and map to a proc for your insert. It would reduce calls but not the cleanest IMHO. If you are processing many users at once then maybe breakup the UOW to act per user only so you have shorter transactions. Is there currently a performance issue or just a future concern? –

Without flattening you are in no different of a scenario than without using EF. I don't understand why thought because you are using DDD you can't introduce entities specific to data mappings.Your Entities can still be used, you just in addition have a new entity with mappings. In fact you can do this without entities in your repository later (repository queries changed objects, flattens and sends to data access layer to call proc)



来源:https://stackoverflow.com/questions/11078126/disconnected-behavior-of-entity-framework-when-updating-object-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!