Clone System in C#. Data.Entity. To the true (non-proxied) class, DynamicProxies?

c# cloning dynamic-proxy entity-framework poco


Possible Duplicate:
EF4 Cast DynamicProxies to underlying object

I'm attempting to learn how to duplicate or change a system. Data.Entity. The class that DynamicProxies belongs to. Eg:

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

I attempted to do the conversion in MyApp.Entities because all of the classes there inherit from BaseEntity:

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

The DynamicProxies were subsequently changed into the genuine 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;

This is because I just want to save this object's plain (POCO) values in the cache instead of using Entity Framework tracking and other features. It must be relatively broad since I will need to be able to accomplish this for all 100 or so Entity types without having to explicitly say = for every property.

5/23/2017 11:51:11 AM

Accepted Answer

this piece could be useful. It covers a variety of data copying techniques. I'm not sure whether it is possible to change an object of type A into an object of type B using these techniques. But it's certainly worth a go.

Since this NuGet download employs the generic repository pattern and memcached to handle the same caching strategy, and since your difficulty with deserializing data seems to be the same there, I would be very interested in the conclusion of this.

8/11/2011 8:29:36 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow