在EF6中存储复杂的分离对象图 [英] Storing a complex detached object graph in EF6

查看:157
本文介绍了在EF6中存储复杂的分离对象图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有现在的情况:



我先使用EF6代码,并创建了一个这样的数据模型:

  public class MainObject {
..some properties
IList< SubObject> SubObjects {get; set;}
}

public class SubObject {
..some properties
IList< SubSubObject> SubSubObjects {get; set;}
}

public class SubObject {
..some属性
IList< SubObject> SubObjects {get; set;}
}

所以基本上我有一个主要对象具有0到多个子对象,并且子对象和子对象之间存在许多关系。



我正在创建一个MVC应用程序,所以我的正常程序流程是用户请求一个基本上使用MainObject作为数据模型的页面。然后,用户与页面进行交互并根据需要进行更改,添加或删除子对象和子对象,然后点击保存。保存后,将对象图发送回控制器,并根据用户在客户端进行的更改查看正确。现在我的问题是如下:



如何以良好的方式将其存储回数据库。



我需要将我的对象附加到上下文中,但是我不知道哪些对象是新的,修改或删除的。



我已经编写了一些部分工作的代码,但是它变得如此丑陋,我真的不想走下去。从数据库中获取相关对象图是否有可能,并将EF与两个图形进行比较,然后将相关更改保存到数据库?



任何有助于使这更平滑的帮助将不胜感激。

解决方案

我最终使用 GraphDiff 为我解决这个问题,它的效果非常好!这真的应该被构建在EF中,但直到此为止,这是一个很好的替代品。



为了解决上面我的问题给出的例子,这将确保分离的图形被正确保存(给定我有一个 MainObject 我要保存称为main):

  context.UpdateGraph(main,map => map 
.AssociatedCollection(m => m.SubObjects,with => with
.AssociatedCollection(s => SubSubObjects)

);

context.SaveChanges();


I have the current scenario:

I'm using EF6 Code first, and have created a data-model something like this:

public class MainObject{
  ..some properties
  IList<SubObject> SubObjects{get;set;}
}

public class SubObject{
  ..some properties
  IList<SubSubObject> SubSubObjects{get;set;}
}

public class SubObject{
  ..some properties
  IList<SubObject> SubObjects{get;set;}
}

So basically I have a main object, that has 0 to many subobjects, and there is a many to many relationship between the subobject and the subsubobjects.

I am creating a MVC application, so my normal program flow is that the user request a page that basically uses a MainObject as it's data model. Then the user interacts with the page and changes, adds or removes subobjects and subsubobjects as he wishes, and then clicks save. On save, the objectgraph is sent back to the controller and it looks correct according to the changes done by the user on the client side. Now my problem is the following:

How to store this back into the database in a good fashion.

I need to attach my object back into the context, but I have no clue which objects are new, modified or deleted.

I have written some code that partially works now, but it's getting so ugly that I really don't want to go down that path. Would it be possible somehow to fetch the relevant object graph from the database, and have EF compare the two graphs toward eachother, and then save the relevant changes to the database?

Any help to make this smoother would be greatly appreciated.

解决方案

I ended up using GraphDiff to solve this for me, and it works just great! This really should be built into EF, but untill it does, this is a great substitute.

To solve the example given in my question above, this will make sure that the detached graph gets saved properly (given I have a MainObject I want to save called main):

context.UpdateGraph(main, map =>map
  .AssociatedCollection( m => m.SubObjects, with => with
    .AssociatedCollection( s => s.SubSubObjects)
  )
);

context.SaveChanges();

这篇关于在EF6中存储复杂的分离对象图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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