EF分离对象和更新导航属性 [英] EF detached object and Updating navigation property

查看:223
本文介绍了EF分离对象和更新导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将Nhibernate从Nhibernate切换到实体框架最新版本。

I recenlty switched from Nhibernate to entity framework latest version.

我有以下情况

订单包含orderDetails列表(导航属性)。此对象处于分离状态。

Order has List of orderDetails (Navigation property). This object is in detached state.

当我插入Order时,会插入带有Orderdetails的订单。

When I insert Order, Order with Orderdetails are inserted.

但是当我更新Order时,不会处理orderdetails通过(ApplyCurrentValues / ApplyOriginalValues,因为它们仅适用于标量属性)

But when I update Order, orderdetails are not handled by either (ApplyCurrentValues/ApplyOriginalValues since they are applicable only to scalar properties)

我看到人们在旧的实体框架中提供解决方案
http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread / 87f63056-7309-41af-8be7-313c20594bde /

I have seen people giving solution in older entity framework http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/87f63056-7309-41af-8be7-313c20594bde/

使用Nhibernate,我们有一行代码来执行此操作,框架处理子对象的插入/更新/删除(导航属性)

With Nhibernate we had one line of code to do this and framework handles insert/update/delete for child objects(navigation properties)

我们是否有任何直接的方法在最新版本的Entity框架中实现分离对象中导航属性的插入/更新?

Do we have any straightforward way to implement Insert/Update for navigation property in detached object in latest version of Entity framework?

 

推荐答案

有几件事你可以在这里考虑:

There are a few things you can consider here:

1。使用称为自跟踪实体的Entity Framework 4.0功能,您的实体足够智能,可以处理此类更新。我想通过这个演练看看是否能满足您的需求:
http:/ /msdn.microsoft.com/en-us/library/ee789839.aspx

1. Using the Entity Framework 4.0 feature called Self-tracking entities, your entities are smart enough to handle this sort of update. I'd sugest looking through this walkthrough to see if that will meet your needs: http://msdn.microsoft.com/en-us/library/ee789839.aspx

2。您可以继续使用链接中的代码。

2. You can continue to use the code in the link.

3。如果在实体中使用外键,在许多情况下,您可以使用ApplyCurrentValues / ApplyOriginalValues,因为这些将检测外键值的更改。话虽这么说,你需要知道在依赖端调用Apply * Values(具有外键的
结束)。我发布了一篇关于如何使用实体集和为一组实体编写扩展方法的博客文章,这些实体有助于在一组实体上调用ApplyCurrentValues:

http://blogs.msdn.com/b/adonet/archive /2010/06/02/working-with-sets-of-self-tracking-entities.aspx

3. If you are using foreign keys in your entities, in many cases you can just use ApplyCurrentValues/ApplyOriginalValues as these will detect a change to the foreign key value. That being said, you need to know to call Apply*Values on the dependent end (the end that has the foreign key). I posted a blog entry on how to work with sets of entities and write extension methods for a group of entities which would help call ApplyCurrentValues on a group of entities: http://blogs.msdn.com/b/adonet/archive/2010/06/02/working-with-sets-of-self-tracking-entities.aspx

杰夫


这篇关于EF分离对象和更新导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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