以编程方式启用/禁用实体代理 [英] Programatically Enabling/Disabling Entity Proxies

查看:136
本文介绍了以编程方式启用/禁用实体代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个新项目中使用具有POCO实体的实体框架4.1。一切都正常,直到我开始使用AppFabric缓存来缓存实体。我开始从与缓存代理对象相关联的缓存中检索entitis。我通过设置ContextOptions.ProxyCreationEnabled = false来修复这个问题。
现在的问题是当我从缓存中获取实体时,我必须使用 ObjectSet.Attach(entity)将实体附加到当前上下文,并使用 ObjectContext.ObjectStateManager.ChangeObjectState(entity,EntityState.Modified)



我的问题是有一种方法来编程启用/禁用代理一套实体?或者换句话说,一种将反序列化实体包含在代理对象中的方法。



如果没有一个很好的方法来做到这一点,现在我正在做的正确?还是有更好的方法?

解决方案

你使用的是正确的方法。编程启用或禁用代理创建的唯一方法是将 ContextOptions.ProxyCreationEnabled 设置为 false



我不认为有将包含反序列化实体的方法包装到代理对象中。问题是代理是从您的实体类型派生的动态创建的type(=在运行时创建的类型)。所以如果你反序列化到你的实体类型,你不能将其转换为其他类型。



可能的工作是使用代理,但禁用 LazyLoading (也在 ContextOptions 中),并手动将实体从加载它们的上下文中分离出来。但它会破坏所有关系,你仍然必须将实体附加到新的上下文并设定其状态。另一个可以工作的解决方案是通过 context.CreateObject 创建新实体,并将所有数据从缓存实体复制到新的实体,但这是我不喜欢的解决方案。 p>

换句话说:一旦你使用分离的实体,你必须手动处理附加和设置状态。如果您要更改,这更糟糕关系



Btw。你提到使用EFv4.1,但你所使用的所有东西都是EFv4。 EFV4.1没有对ObjectContext API进行任何更改,它添加了不同的DbContext API,除非您将 DbContext 转回 ObjectContext 你正在使用EFv4。如果您将 DbContext 转回 ObjectContext ,那么您应该检查DbContext API,因为它提供了相当于附加 ChangeObjectState => DbSet.Attach ande context。条目(实体).State


I am using the Entity Framework 4.1 with POCO entities in a new project. Everything was working fine until I began caching the entities using AppFabric Caching. I started getting erros retrieving the entitis from the cache related to deserializing the proxy objects. I fixed this problem by setting ContextOptions.ProxyCreationEnabled = false. The issue now is when I get entities back from the cache, I have to attach the entity to the current context using ObjectSet.Attach(entity) and add them to the state manager using ObjectContext.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified).

My question is is there a way to programatically enable/disable the proxy for a set of entities? Or in other words a way to wrap the deserialized entity in the proxy object.

If there is not a good way to do this, is the way I am doing it now correct? Or is there a better way?

解决方案

You are using it the correct way. The only way to programatically enable or disable proxy creation is setting ContextOptions.ProxyCreationEnabled to false or true as you do at the moment.

I don't think there is the way to wrap deserialized entity into proxy object. The problem is that the proxy is dynamically created type (= type created at runtime) derived from your entity type. So if you deserialize it to your entity type you can't cast it to other type.

What can probably work is using proxies but disabling LazyLoading (also in ContextOptions) and manually detaching entities from the context which loads them. But it will break all relation and you will still have to attach the entity to the new context and set its state. Another solution which can work is creating new entity by context.CreateObject and copying all data from cached entity to the new one but that is solution which I don't like.

Said in other words: Once you are working with detached entities you must deal with attaching and setting state manually. This is even worse if you are going to change relations.

Btw. you mentioned using EFv4.1 but all the stuff you are using is EFv4. EFv4.1 didn't make any change to ObjectContext API, it added different DbContext API so unless you are casting DbContext back to ObjectContext you are using EFv4. If you cast DbContext back to ObjectContext then you should check DbContext API because it offers equivalents of Attach or ChangeObjectState => DbSet.Attach ande context.Entry(entity).State.

这篇关于以编程方式启用/禁用实体代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆