AutoMapper 不会取消代理 NHibernate 实体
AutoMapper does not unproxy NHibernate entity
考虑这个实体:
public class CondRule
{
public virtual decimal Id { get; set; }
public virtual string Name { get; set; }
public virtual CondRuleType RuleType { get; set; }
public virtual string Statement { get; set; }
}
和CondRuleType
是:
public class CondRuleType
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
显然 CondRule
和 CondRuleType
实体之间存在一对一关系。
我还有 CondRuleDto
:
public class CondRuleDto
{
public decimal Id { get; set; }
public string Name { get; set; }
public CondRuleType RuleType { get; set; }
}
我使用 AutoMapper
:
将 CondRule
映射到 CondRuleDto
Mapper.CreateMap<CondRule, CondRuleDto>();
当我调用 Session.Get
通过 id 获取 CondRule
并将结果映射到 CondRuleDto
时,AutoMapper 不解析代理(此处为 RuleType
)。
这是我的代码:
var condRule = Session.Get<CondRule>(id);
var condRuleDto = Mapper.Map<CondRuleDto>(condRule);
当我观看 condRuleDto 时,RuleType
属性 是一个 NHibernate 代理。我希望 AutoMapper
将 RuleType
代理映射到 POCO。如何实现?
PS: 我不得不提一下,当我使用查询和使用自动映射器的 Project
时,它会产生一个没有代理的列表(我知道Project
让这一切发生了。可能我需要像 Project
这样的东西在 Session.Get
之后使用):
Session.Query<CondRule>().Project().To<CondRuleDto>().ToList()
转换不会改变底层对象(即即使您将它的实例映射到另一个 CondRuleType
类型的 属性,您的 CondRuleType
仍然是一个代理)。
您似乎需要创建一个自定义映射,其中映射 CondRule.RuleType
创建一个新的 CondRuleType
实例。
考虑这个实体:
public class CondRule
{
public virtual decimal Id { get; set; }
public virtual string Name { get; set; }
public virtual CondRuleType RuleType { get; set; }
public virtual string Statement { get; set; }
}
和CondRuleType
是:
public class CondRuleType
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
显然 CondRule
和 CondRuleType
实体之间存在一对一关系。
我还有 CondRuleDto
:
public class CondRuleDto
{
public decimal Id { get; set; }
public string Name { get; set; }
public CondRuleType RuleType { get; set; }
}
我使用 AutoMapper
:
CondRule
映射到 CondRuleDto
Mapper.CreateMap<CondRule, CondRuleDto>();
当我调用 Session.Get
通过 id 获取 CondRule
并将结果映射到 CondRuleDto
时,AutoMapper 不解析代理(此处为 RuleType
)。
这是我的代码:
var condRule = Session.Get<CondRule>(id);
var condRuleDto = Mapper.Map<CondRuleDto>(condRule);
当我观看 condRuleDto 时,RuleType
属性 是一个 NHibernate 代理。我希望 AutoMapper
将 RuleType
代理映射到 POCO。如何实现?
PS: 我不得不提一下,当我使用查询和使用自动映射器的 Project
时,它会产生一个没有代理的列表(我知道Project
让这一切发生了。可能我需要像 Project
这样的东西在 Session.Get
之后使用):
Session.Query<CondRule>().Project().To<CondRuleDto>().ToList()
转换不会改变底层对象(即即使您将它的实例映射到另一个 CondRuleType
类型的 属性,您的 CondRuleType
仍然是一个代理)。
您似乎需要创建一个自定义映射,其中映射 CondRule.RuleType
创建一个新的 CondRuleType
实例。