开发者

Problem updating foreign key relationship in EF 4.0 by setting Id

I'm trying to update a foreign key relationship in EntityFramework 4 (with a m开发者_运维百科icrosoft sql server database) via a foreign key.

I load the object using .Single() and then try set to the the Object.ExampleId (The Example table is a reference table that corresponds to an enumeration in C#). Then I call SaveChanges() on the context.

Doing so works locally, but on production servers it fails and appears to hang. Using debug statements I've narrowed the hang down to the line where I actually set the Id.

I was able to fix the problem by loading the entire Example object and doing Object.Example = loadedExample instead of Object.ExampleId = exampleId.

My question is why is this the case? Other places I have been able to update relationships just by setting the Id. Is this a bad practice?


The problem turned out to be that setting the FK id caused entity framework to load all the members of the foreign key relationship that had that id. On our staging and live servers the number of items in that relationship was a lot higher.

This caused the request to timeout and fail.

The solution was here: EF4 and undesired loading of related collection with AddObject

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜