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;
この理由は、このオブジェクトからすべてのEntity Frameworkトラッキングなどを削除し、そのプレーン(POCO)プロパティをキャッシュに保存するためです。そして、100程度のエンティティクラスすべてに対してこれを実行できる必要があるため、すべてのプロパティに対して手動でobject1.foo = object2.fooと言わずに、かなり一般的である必要があります。