【发布时间】:2015-09-17 00:54:51
【问题描述】:
考虑这个实体:
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>();
当我通过 id 调用 Session.Get 以获取 CondRule 并将结果映射到 CondRuleDto 时,AutoMapper 不会解析代理(此处为 RuleType)。
这是我的代码:
var condRule = Session.Get<CondRule>(id);
var condRuleDto = Mapper.Map<CondRuleDto>(condRule);
当我观看 condRuleDto 时,RuleType 属性是 NHibernate 代理。我希望 AutoMapper 将 RuleType 代理映射到 POCO。如何使这项工作?
PS:我不得不提一下,当我使用查询并使用 automapper 的 Project 时,它会产生一个没有代理的列表(我知道 Project 会发生这种情况。可能是我需要Project 之类的东西在Session.Get 之后使用):
Session.Query<CondRule>().Project().To<CondRuleDto>().ToList()
【问题讨论】:
标签: c# nhibernate asp.net-web-api automapper