安装并从上下文菜单中EF4.1正确分离实体 [英] Attaching and detaching entities from context correctly in EF4.1

查看:121
本文介绍了安装并从上下文菜单中EF4.1正确分离实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实施实体的缓存机制。并与缓存我需要从当前上下文脱离实体我把之前在缓存中,并附加回了新的环境时,我从缓存中得到它正确,无缝地使用的实体。 (我的上下文生存是每个HTTP请求)

I am trying to implement caching mechanism for entities. And to use the entities correctly and seamlessly with the caching i need to detach the entity from the current context before i put it in a cache and attach it back the the new context when i get it from the cache. (My context lifetime is per http request)

的要求是 -


  1. 所有这一切都与它相关联(我已经填充),当实体分离不应该被删除。导航性能

  2. 我可以更新缓存的项目,如果我想(所以他们正确连接到新的环境是很重要的)。

这是我在创造一个EntityCache类的尝试 - (ServerCache这里是我的包装类推对象ASP.NET缓存)

This is my attempt at creating an EntityCache class - (ServerCache here is my wrapper class that pushes the object to ASP.NET cache)

public static class EntityCache
    {
        private static DbContext context
        {
            get
            {
                return (DbContext)HttpContext.Current.Items[ObjectKeys.ContextRequestItemKey];
            }
        }

        private static void Detach(object entity)
        {
            var trackedEntity = entity as IEntityWithChangeTracker;
            trackedEntity.SetChangeTracker(null);
            ((IObjectContextAdapter)context).ObjectContext.Detach(entity);
        }

        private static void Attach(object entity)
        {
            ((IObjectContextAdapter)context).ObjectContext.Attach((IEntityWithKey)entity);
        }

        public static void Remove(string key)
        {
            ServerCache.Remove(key);
        }

        public static object Get(string key)
        {
            object output = ServerCache.Get(key);
            if (output != null)
                Attach(output);
            return output;
        }

        public static void ShortCache(String key, object data)
        {
            if (data != null)
            {
                Detach(data);
                ServerCache.ShortCache(key, data);
            }
        }

        public static void LongCache(String key, object data)
        {
            if (data != null)
            {
                Detach(data);
                ServerCache.LongCache(key, data);
            }
        }
    }

当我在缓存中把一个实体它的类型是DynamicProxy而不是真正的类。

When i put an entity in the cache it is of type DynamicProxy and NOT the real class.

附加不工作在所有 - 我得到一个异常,我无法案例对象的类型Dynamic_ {} blahblah向IEntityWithKey的

Attaching doesnt work at all - i get an exception that i cannot case object that is of type Dynamic_{blahblah} to IEntityWithKey.

我刚刚看到附加和在线分离的这些例子和审判他们,我愿意接受任何新的实施,这里的连接/断开的方法。

I just saw these examples of attach and detach online and tried them, I am open to any new implementation of the Attach/Detach methods here.

感谢您。

跟进的问题 -

context.Entry(entity).State = EntityState.Detached;

的作品,但使所有的导航属性所加载NULL,我们如何让它保持导航性能而不是NULL替代(或失去)他们,当我们从背景中分离。

Works, but makes all the navigational properties that are loaded NULL, how do we make it keep the navigational properties and NOT replace(or lose) them with NULL when we detach from context.

推荐答案

IEntityWithKey 是其他类型的实体界面。这是大的实体。例如 EntityObject 实现这个接口。这些实体不被认为是POCO,不受的DbContext API支持。

IEntityWithKey is interface for other types of entities. It is for "big" entities. For example EntityObject implement this interface. These entities are not considered as POCO and are not supported by DbContext API.

如果你想使用 IEntityWithKey 您的类必须实现它 - 它不是东西会自动发生。

If you want to use IEntityWithKey your classes must implement it - it is not something that would happen automatically.

的DbContext API正确附着应该是:

Correct attaching with DbContext API should be:

dbContext.Set(typeof(entity)).Attach(entity); 

和这个希望也应该工作:

and this should hopefully also work:

dbContext.Entry(entity).State = EntityState.Unchanged;

的DbContext API正确拆卸应该是:

Correct detaching with DbContext API should be:

dbContext.Entry(entity).State = EntityState.Detached;

此外,最好是你的通用方法,而不是对象

这篇关于安装并从上下文菜单中EF4.1正确分离实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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