免责声明:我已经为这个问题创建了一个通用的解决方案。我在寻找解决方案时发现了这个老问题,所以我想我会在这里分享我的解决方案,以帮助任何可能在同一个问题上遇到困难的人。
我遇到了同样的问题:我需要从 Entity Framework 获取一些东西,然后使用 ASP.NET Web Api 将其序列化为 XML。我已经尝试禁用延迟加载和代理创建并使用 Include(),但是除了最基本的类层次结构之外的任何东西都会导致需要几分钟才能执行的巨大 SQL 查询。我发现使用延迟加载和递归引用每个属性比一次加载所有树要快很多很多倍,所以我想我需要一种方法来延迟加载所有内容,以 POCO 的形式获取它,然后序列化它。
我使用 Gert Arnold 的 this answer 作为此解决方案的基础,然后从那里开始工作。
我在 DBContext 中创建了一个 Unproxy 方法,该方法采用(代理)类实例(例如,您将从 DbContext.Find(id) 返回的东西)并将该实体作为实际的 POCO 类型返回,每个属性、子属性等已完全加载并准备好进行序列化。
Unproxy 方法和一些只读字段:
readonly Type ignoreOnUnproxyAttributeType = typeof(IgnoreOnUnproxyAttribute);
readonly string genericCollectionTypeName = typeof(ICollection<>).Name;
public T UnProxy<T>(T proxyObject) where T : class
{
// Remember the proxyCreationEnabled value
var proxyCreationEnabled = Configuration.ProxyCreationEnabled;
try
{
Configuration.ProxyCreationEnabled = false;
T poco = Entry(proxyObject).CurrentValues.ToObject() as T; // Convert the proxy object to a POCO object. This only populates scalar values and such, so we have to load other properties separately.
// Iterate through all properties in the POCO type
foreach (var property in poco.GetType().GetProperties())
{
// To prevent cycles, like when a child instance refers to its parent and the parent refers to its child, we'll ignore any properties decorated with a custom IgnoreOnUnproxyAttribute.
if (Attribute.IsDefined(property, ignoreOnUnproxyAttributeType))
{
property.SetValue(poco, null);
continue;
}
dynamic proxyPropertyValue = property.GetValue(proxyObject); // Get the property's value from the proxy object
if (proxyPropertyValue != null)
{
// If the property is a collection, get each item in the collection and set the value of the property to a new collection containing those items.
if (property.PropertyType.IsGenericType && property.PropertyType.Name == genericCollectionTypeName)
{
SetCollectionPropertyOnPoco<T>(poco, property, proxyPropertyValue);
}
else
{
// If the property is not a collection, just set the value of the POCO object to the unproxied (if necessary) value of the proxy object's property.
if (proxyPropertyValue != null)
{
// If the type of the property is one of the types in your model, the value needs to be unproxied first. Otherwise, just set the value as is.
var unproxiedValue = (ModelTypeNames.Contains(property.PropertyType.Name)) ? SafeUnproxy(proxyPropertyValue) : proxyPropertyValue;
property.SetValue(poco, unproxiedValue);
}
}
}
}
return poco; // Return the unproxied object
}
finally
{
// Zet ProxyCreationEnabled weer terug naar de oorspronkelijke waarde.
Configuration.ProxyCreationEnabled = proxyCreationEnabled;
}
}
ModelTypeNames 是我添加到我的 DBContext 的一个属性,它只返回模型中使用的所有类型。这样我们就知道我们需要取消代理哪些类型:
private Collection<string> modelTypeNames;
private Collection<string> ModelTypeNames
{
get
{
if (modelTypeNames == null)
{
// We'll figure out all the EF model types by simply returning all the type arguments of every DbSet<> property in the dbContext.
modelTypeNames = new Collection<string>(typeof(VerhaalLokaalDbContext).GetProperties().Where(d => d.PropertyType.Name == typeof(DbSet<>).Name).SelectMany(d => d.PropertyType.GenericTypeArguments).Select(t => t.Name).ToList());
}
return modelTypeNames;
}
}
要处理 ICollection 属性,我们需要首先实例化一个新的泛型集合(我使用反射来创建具有正确类型参数的 HashSet),遍历所有值,取消代理每个值并添加它到新的 HashSet,然后用作 POCO 属性的值。
private void SetCollectionPropertyOnPoco<T>(T poco, PropertyInfo property, dynamic proxyPropertyValue) where T : class
{
// Create a HashSet<> with the correct type
var genericTypeArguments = ((System.Type)(proxyPropertyValue.GetType())).GenericTypeArguments;
var hashSetType = typeof(System.Collections.Generic.HashSet<>).MakeGenericType(genericTypeArguments);
var hashSet = Activator.CreateInstance(hashSetType);
// Iterate through each item in the collection, unproxy it, and add it to the hashset.
foreach (var item in proxyPropertyValue)
{
object unproxiedValue = SafeUnproxy(item);
hashSetType.GetMethod("Add").Invoke(hashSet, new[] { unproxiedValue }); // Add the unproxied value to the new hashset
}
property.SetValue(poco, hashSet); // Set the new hashset as the poco property value.
}
请注意,我调用的是 SafeUnproxy 而不是 Unproxy。这是因为类型推断存在一个奇怪的问题。通常,当您将代理对象传递给 Unproxy() 时,类型推断会推断 T 是您真正想要的 POCO 类型,而不是 dataproxy 的类型(看起来像 YourModelPocoType_D0339E043A5559D04303M3033 等的类型)。但是,偶尔它确实将 T 推断为 dataproxy 类型,这会炸毁
T poco = Entry(proxyObject).CurrentValues.ToObject() as T;
行,因为poco对象不能强制转换为代理类型,导致as操作符返回null。为了解决这个问题,SafeUnproxy 使用显式类型参数调用 Unproxy 方法,而不是依赖于推理:它检查您传递给它的参数的类型,如果命名空间是 System.Data.Entity.DynamicProxies,它将使用类型的BaseType(在动态代理类型的情况下是对应的 POCO 类型)作为泛型类型参数。
private object SafeUnproxy(dynamic item)
{
// ProxyCreation is off, so any reference or collection properties may not yet be loaded. We need to make sure we explicitly load each property from the db first.
ExplicitlyLoadMembers(item);
// Figure out the right type to use as the explicit generic type argument
var itemType = item.GetType();
Type requiredPocoType = (itemType.Namespace == "System.Data.Entity.DynamicProxies") ?
itemType.BaseType :
itemType;
// Call Unproxy using an explicit generic type argument
var unproxiedValue = typeof(VerhaalLokaalDbContext).GetMethod("UnProxy").MakeGenericMethod(requiredPocoType).Invoke(this, new[] { item });
return unproxiedValue;
}
确保从数据库中加载每个属性只需遍历对象的属性并检查 IsLoaded:
private void ExplicitlyLoadMembers(dynamic item)
{
foreach (var property in ((Type)item.GetType()).GetProperties())
{
DbEntityEntry dbEntityEntry = Entry(item);
var dbMemberEntry = dbEntityEntry.Member(property.Name);
// If we're dealing with a Reference or Collection entity, explicitly load the properties if necessary.
if (dbMemberEntry is DbReferenceEntry)
{
if (!dbEntityEntry.Reference(property.Name).IsLoaded)
{
dbEntityEntry.Reference(property.Name).Load();
}
}
else if (dbMemberEntry is DbCollectionEntry)
{
if (!dbEntityEntry.Collection(property.Name).IsLoaded)
{
dbEntityEntry.Collection(property.Name).Load();
}
}
}
}
最后,IgnoreOnUnproxyAttribute 用来避免循环:
[System.AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
sealed class IgnoreOnUnproxyAttribute : Attribute
{
}
用法如下:
MyDbContext db = new MyDbContext();
public Story Get(int storyId)
{
var lazyStory = db.Stories.SingleOrDefault(s => s.Id == storyId);
var unproxied = db.UnProxy(lazyStory);
return unproxied;
}
由于所有反射都在进行,因此性能并不出色,但执行时间平均只比延迟加载实体、迭代其所有属性、然后序列化动态代理时稍长(即不到一秒)本身。此外,它比使用非常慢且容易出错的 Include() 时快得多。
希望它对某人有所帮助。