开发者

Entity Framework 4: How do I get the original values of a child object when saving a parent?

I've seen lots of posts on SE relating to this, but none have answered the question satisfactorily. If there is a post that does answer this (with an actual code example) then please point me in that direction.

I need to write information to a log when saving an object. I need to know the original values and the new values. This is very easy for the parent object, and it is even fairly easy to get the new values on any changed child objects. The challenge is getting the original values of the child object.

For instance, a user changes a child object via a drop-down list. This changes the value of the foreign key on the parent. When saving, I need to write the textual description (the ToString() value or some other value) of the changed entity in the log, not the value of开发者_JS百科 the foreign key.

The ObjectStateEntry contains the current values and original values of the parent, but how do I get the current and original values of the changed child object?

It seems like this is something that should be possible, but is either much too difficult to accomplish, or has been overlooked by the Microsoft design team.

Thanks in advance for any help.


The same way you always get it: You look it up. Remember, in your case the "child object" might not even be loaded from the DB. There is no requirement to do so before changing the FK value on the "parent."

It doesn't sound like you actually changed the "child object" itself. Rather, you just changed the "parent" to point at a different child object.

In this case, I'd use Context.GetObjectByKey() to pull the object based on the original FK value. This grabs it from memory if it happens to be loaded, and from the DB if it doesn't.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜