C# Fluent nHibernate在返回对象时除去代理成员的可能方法
我有一个班级和这个班级的地图C# Fluent nHibernate在返回对象时除去代理成员的可能方法,c#,.net,nhibernate,fluent-nhibernate,lazy-loading,C#,.net,Nhibernate,Fluent Nhibernate,Lazy Loading,我有一个班级和这个班级的地图 public class TestClass { public virtual Subcategory SubCategory { get; set; } } public TestClassMuMap() { Id(e => e.Id).Column("ID").GeneratedBy.Assigned(); References(x => x.SubCategory).Column("Subcategory"); } 在接
public class TestClass
{
public virtual Subcategory SubCategory { get; set; }
}
public TestClassMuMap()
{
Id(e => e.Id).Column("ID").GeneratedBy.Assigned();
References(x => x.SubCategory).Column("Subcategory");
}
在接收到的对象中,我找到了新成员子类别proxyf528587c5459469ba2347093600432d8
我怎样才能摆脱它 不是。LazyLoad()
应该这样做:
References(x => x.SubCategory).Not.LazyLoad().Column("Subcategory");
但是懒散是件好事,检查一下你是否真的在获取代理方面有问题
什么是懒虫
集合(数组除外)可以延迟初始化,这意味着
只有当应用程序
需要访问它。初始化对用户是透明的
因此,应用程序通常不需要担心这一点
在上的中阅读更多内容,您将发现一个扩展类NHibernateUnProxyExtension
,它将为您完成以下工作:
public static class NHibernateUnProxyExtension
{
/// <summary>
/// Recursively removes NHibernate proxies from an object. Do not use session.Save(objt) or session.Update(objt) after unproxying, you might lose important data
/// </summary>
public static void UnProxy(this object objt)
{
for (int i = 0; i < objt.GetType().GetProperties().Count(); i++)
{
try
{
PropertyInfo propertyInfo = objt.GetType().GetProperties()[i];
var propValue = propertyInfo.GetValue(objt, null);
if (propValue.IsProxy())
{
System.Type objType = NHibernateProxyHelper.GetClassWithoutInitializingProxy(propValue);
//Creates a new NonProxyObject
object NonProxyObject = Activator.CreateInstance(objType);
//Copy everything that it can be copied
foreach (var prop in propValue.GetType().GetProperties())
{
try
{
object a = prop.GetValue(propValue);
NonProxyObject.GetType().GetProperty(prop.Name).SetValue(NonProxyObject, a);
}
catch { }
}
//Change the proxy to the real class
propertyInfo.SetValue(objt, NonProxyObject);
}
//Lists
if (propValue.GetType().IsGenericType && propValue.GetType().GetGenericTypeDefinition() == typeof(PersistentGenericBag<>))
{
try
{
int count = (int)(propValue.GetType().GetProperty("Count").GetValue(propValue));
}
catch { propertyInfo.SetValue(objt, null); }
}
if (propValue.GetType().Assembly.GetName().Name != "mscorlib" &&
propValue.GetType().Assembly.GetName().Name != "NHibernate")
{
// user-defined!
propValue.UnProxy();
}
}
catch { }
}
}
}
公共静态类nhibernateunproxy扩展
{
///
///递归地从对象中删除NHibernate代理。不要使用session.Save(objt)或session.Update(objt)。取消验证后,可能会丢失重要数据
///
公共静态无效取消固定(此对象对象对象)
{
对于(int i=0;i
懒散又意味着什么?