How do I override a computed value with EntityFramework 4.1 DbContext API
I have a rowversion (timestamp
) column which is set to computed in my EF designer.
Setting the value in code, via the direct property like
myEnt.rowversion = screenRowVersion;
has now effect when SaveChanges(开发者_如何学Python)
is called, a trace to SQLServer shows that the original value of rowversion is used.
Is it possible to have the DbContext API accept an external computed value?
EF doesn't allow overriding computed values - it always uses original values loaded from the database. There is workaround for this where you cheat EF and change the original value tracked by the context:
context.Entry(myEnt).OriginalValues["rowversion"] = screenRowVersion;
But anyway in case of timestamp it is not needed. You have the old timestamp and a new timestamp so you can compare them in your application without necessary roundtrip to database.
精彩评论