开发者

Set Related Entity Id on Detached POCO Proxy

I have a detached POCO proxy from EF 4.0 (detached meaning the Obje开发者_高级运维ctContext has disposed).

I have a property MyRelatedEntityId which is a FK for the navigation property MyRelatedEntity.

As it happens, I know what value I want to set for MyRelatedEntityId, so I try to set it manually....but the detached POCO proxy throws an exception that the ObjectContext has already disposed.

How can I do this or is there a workaround for this?


You must turn off lazy loading. I think once you set FK property, dynamic proxy will try to load related entity and it fails because of disposed context.

Edit:

I just checked this problem because as you mentioned lazy loading should not be triggered. The problem is autogenerated code which uses Fixup methods to set up a bidirectional relation. If you loaded previous related entity and you change FK it sets related entity to null. This triggers the fixup method which tries to remove the current entity from the previous related entity's childs. If you didn't load them before disposing context you will get an exception. Just try to debug the code including generated one.

The solution is either:

  • Turn off lazy loading for this scenario
  • Remove navigation collection from related entity
  • Modify T4 template so it doesn't generate fixup methods


Did you detatch the entity before the ObjectContext was disposed? Have you tried setting it after attaching the entity to a new ObjectContext?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜