C# 将 System.Data.Entity.DynamicProxies 克隆到实际(非代理)类?

发布于 2024-11-24 14:31:45 字数 1211 浏览 1 评论 0原文

可能的重复:
EF4 将 DynamicProxies 转换为底层对象

我正在尝试弄清楚如何克隆或将 System.Data.Entity.DynamicProxies 转换为其实际的类。例如:

System.Data.Entity.DynamicProxies.Currency_F4008E27DE_etc is the proxy class
MyApp.Entities.Currency is the real class

MyApp.Entities 中的所有类都继承自 BaseEntity,因此我尝试在那里进行转换:

public abstract partial class BaseEntity
{
    public T ShallowCopy<T>() where T : BaseEntity
    {
        return this.MemberwiseClone() as T;
    }
    // other BaseEntity properties not relevent here
}

然后将 DynamicProxies 转换为真正的类:

// this returns a DynamicProxies class
Currency currency = LookupDefaultCurrency(); 
// this one needs to return a Entities.Currency class 
// (but currently returns a DynamicProxies class too
Currency pocoCurrency = (Currency)currency.ShallowCopy<Currency>();
HttpRuntime.Cache[key] = pocoCurrency;

这样做的原因是我想从中删除所有实体框架跟踪等这个对象并只将其普通(POCO)属性存储在缓存中。我需要能够对所有 100 个左右的实体类执行此操作,因此它必须相当通用 - 无需为每个属性手动指定 object1.foo = object2.foo。

Possible Duplicate:
EF4 Cast DynamicProxies to underlying object

I'm trying to figure out how to clone or convert a System.Data.Entity.DynamicProxies into it's actual class. Eg:

System.Data.Entity.DynamicProxies.Currency_F4008E27DE_etc is the proxy class
MyApp.Entities.Currency is the real class

All of the classes in MyApp.Entities inherit from BaseEntity, so I tried to do the converting there:

public abstract partial class BaseEntity
{
    public T ShallowCopy<T>() where T : BaseEntity
    {
        return this.MemberwiseClone() as T;
    }
    // other BaseEntity properties not relevent here
}

And then converting the DynamicProxies into the real class:

// this returns a DynamicProxies class
Currency currency = LookupDefaultCurrency(); 
// this one needs to return a Entities.Currency class 
// (but currently returns a DynamicProxies class too
Currency pocoCurrency = (Currency)currency.ShallowCopy<Currency>();
HttpRuntime.Cache[key] = pocoCurrency;

The reason for this is that I want to remove all Entity Framework tracking and etc from this object and just store its plain (POCO) properties in the cache. And I will need to be able to do this for all 100 or so Entity classes, so it has to be reasonably generic - without manually saying object1.foo = object2.foo for every single property.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮光沉寂 2024-12-01 14:31:45

也许这篇文章会有所帮助。它讨论了克隆数据的几种方法。我不确定这些方法是否可以用于将 A 类型的对象转换为 B 类型的对象。但这绝对值得一试。

我对此结果非常感兴趣,因为 此 NuGet 包 也使用通用存储库模式和 memcached 来解决相同的缓存技术,并且在反序列化数据时您的问题似乎是相同的。

Maybe this article is helpful. It discusses several methods for cloning data. I'm not sure if these methods can be used to convert an object of type A to an object of type B. But it's definitely worth a try.

I would be very interested in the outcome of this, since this NuGet package also uses the generic repository pattern and memcached to address the same caching technique and your problem seems to be same there when deserializing data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文