从实体框架代理获取底层的实体对象

从实体框架代理获取底层的实体对象

本文介绍了从实体框架代理获取底层的实体对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 DbEntityEntry.Entity 得到它拥有的一个实体。这将返回该实体的实体框架代理。

我如何访问底层对象作为它的原始类型,而不是代理?

另外我需要动态地尝试投代理的实体类型。这里是一个开始。

  VAR theEntityType = entityEntry.Entity;如果(theEntityType.BaseType = NULL&放大器;!&安培; entityType.Namespace ==System.Data.Entity.DynamicProxies)
       theEntityType = entityType.BaseType;//现在我需要转换为正确的类型
VAR entityObject =(theEntityType)entityEntry.Entity; //这不会起作用,因为`theEntityType`是动态的。
//我的entites还没有实现IConvertible


解决方案

首先,我应该说没有基本对象。代理不的的实体对象(Decorator模式),它的导出的从它(继承)。因此,我们不能解开的实体,我们只能的转换的代理为基本对象。转换(与铸造)总是创建一个新的对象。

有关这种转换,我们可以利用这一点大部分的时间,顺便代理由EF返回其实的编译时类型的代理的是基本类型。也就是说,如果一个代理被输入作为参数的通用方法,所述通用参数将被推断为基本类型。此功能使我们能够创造出你想要做什么的方法:

  T UnProxy< T>(的DbContext背景下,T proxyObject)其中T:类
{
    VAR proxyCreationEnabled = context.Configuration.ProxyCreationEnabled;
    尝试
    {
        context.Configuration.ProxyCreationEnabled = FALSE;
        ŧPOCO = context.Entry(proxyObject).CurrentValues​​.ToObject()为T;
        返回POCO;
    }
    最后
    {
        context.Configuration.ProxyCreationEnabled = proxyCreationEnabled;
    }
}

说明

代理对象进入方法。其类型被推断为基POCO类型。现在我们可以暂时上下文关闭 ProxyCreationEnabled 和代理对象复制到其基地POCO类型的对象。这个复制操作感激使用一些EF功能。

I have an entity by getting it from DbEntityEntry.Entity. This returns the Entity Framework proxy for the entity.

How do I access the underlying object as it's original type instead of the proxy?

Alternatively I need to dynamically try to cast the proxy to the entity type. Here's a start..

var theEntityType = entityEntry.Entity;

if (theEntityType.BaseType != null && entityType.Namespace == "System.Data.Entity.DynamicProxies")
       theEntityType = entityType.BaseType;

// Now I need to cast to the correct type
var entityObject = (theEntityType)entityEntry.Entity; // THIS WON'T WORK BECAUSE `theEntityType` is dynamic.
// My entites also don't implement IConvertible
解决方案

First I should say there is no underlying object. A proxy doesn't wrap an entity object (decorator pattern), it derives from it (inheritance). So we can't unwrap the entity, we can only convert a proxy to a base object. Conversion (contrary to casting) always creates a new object.

For this conversion, we can exploit the fact that most of the time, by the way proxies are returned by EF, the compile time type of a proxy is the base type. That is, if a proxy is entered as an argument to a generic method, the generic parameter will be inferred as the base type. This feature allows us to create a method that does what you want:

T UnProxy<T>(DbContext context, T proxyObject) where T : class
{
    var proxyCreationEnabled = context.Configuration.ProxyCreationEnabled;
    try
    {
        context.Configuration.ProxyCreationEnabled = false;
        T poco = context.Entry(proxyObject).CurrentValues.ToObject() as T;
        return poco;
    }
    finally
    {
        context.Configuration.ProxyCreationEnabled = proxyCreationEnabled;
    }
}

Explanation

The proxy object enters the method. Its type is inferred as the base POCO type. Now we can temporarily turn off ProxyCreationEnabled on the context and copy the proxy object to an object of its base POCO type. This copy action gratefully uses a few EF features.

这篇关于从实体框架代理获取底层的实体对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 17:16