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

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

问题描述

我在新项目中将Entity Framework 4.1与POCO实体一起使用.一切工作正常,直到我开始使用AppFabric Caching缓存实体.我开始从与反序列化代理对象有关的高速缓存中检索到实体信息而犯下错误.我通过设置ContextOptions.ProxyCreationEnabled = false来解决此问题. 现在的问题是,当我从缓存取回实体时,必须使用 ObjectSet.Attach(entity)将实体附加到当前上下文,然后使用 ObjectContext将它们添加到状态管理器中. ObjectStateManager.ChangeObjectState(entity,EntityState.Modified).

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 programmatically 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?

推荐答案

您正以正确的方式使用它.以编程方式启用或禁用代理创建的唯一方法是像现在一样将ContextOptions.ProxyCreationEnabled设置为falsetrue.

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.

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

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.

顺便说一句.您提到使用EFv4.1,但您使用的所有内容都是EFv4. EFv4.1并未对ObjectContext API进行任何更改,而是添加了不同的DbContext API,因此,除非您将DbContext强制转换回ObjectContext,否则您将使用EFv4.如果将DbContext强制转换回ObjectContext,则应检查DbContext API,因为它提供了AttachChangeObjectState => DbSet.Attach ande context.Entry(entity).State的等效项.

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天全站免登陆