有沒有辦法從ChangeTracker
(而不是原始值)獲取原始實體本身?如何從ChangeTracker獲取原始實體
如果State
是Modified
,那麼我想我可以做到這一點:
// Get the DbEntityEntry from the DbContext.ChangeTracker...
// Store the current values
var currentValues = entry.CurrentValues.Clone();
// Set to the original values
entry.CurrentValues.SetValues(entry.OriginalValues.Clone());
// Now we have the original entity
Foo entity = (Foo)entry.Entity;
// Do something with it...
// Restore the current values
entry.CurrentValues.SetValues(currentValues);
但這似乎不是很好,而且我敢肯定有問題,難道是我不知道關於...有更好的方法嗎?
我使用實體框架6.
我可能缺少一個微妙的東西,但我相信我知道如何做到這一點(這只是獲得原始值,對嗎?)我想要一個實際的強類型表示的原始實體 - 而不僅僅是原始值。 – Eric 2013-02-21 21:37:49
@Eric你想擁有原始屬性值的實體對象? – 2013-02-21 21:39:14
是的,這是正確的。也許一種方法來建立一個給定的一組原始值... – Eric 2013-02-21 21:40:06