开发者

Entity Framework v1 and Cached POCO update

I am using EF v1 and Veloci开发者_如何学JAVAty cache in my .NET 3.5 application. My cached objects are a representation of the Entity model in database. So I decided to use EF v1 with POCO adapters. So there's an entity access layer that reads the object from cache and if not present reads it from database and caches it immediately. Now in the use case where a update is required, the dirty POCO needs to be updated in database. Now I cant directly use the POCO adapters and proxies here since they need a backing objects that are tracking changes. I never hydrated the adapter object from the framework so change detection is not happening. The easiest option I see is to:

  1. Read the entity before update. Set the adapter values from POCO. Let change detection take its course. This does a select before every update which could be costly given many associated entities.
  2. Create a new Entity object. Set the Entity key and properties from POCO. Save Changes. This does unnecessary writes even when property/associated entity has not been changed.

I hope I was clear in the problem statement. Please suggest.


Actually turns out 1. and 2. are the same. You need to do a read of the entity entirely (alongwith associations) to make sure changes in your detached object are translated to the entity context. The caveats of this approach are: 1. Read before update 2. Stale writes (since all properties from detached object get updated, we might end up updating a staler value since we dont what really we changed)

I dont think there is any way to avoid 1. But if 2. is critical to your business requirements (meaning you cant afford to overwrite with stale values, keep track of fields you are changing by "IsChangedProperty" and update only those fields. This is very painful but unfortunately with detached objects neither EF v4 nor v1 gives you any respite :(

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜