且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EF分离对象和更新导航属性

更新时间:2023-12-05 11:58:52

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

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

杰夫