【发布时间】:2016-08-31 15:17:43
【问题描述】:
我有很多 POCO 类,每个类都包含多个虚拟属性。像这样的:
public class Policy
{
public int Id { get; set; }
public int EntityId { get; set; }
public int ProgramId { get; set; }
public string PolicyNumber { get; set; }
public DateTime EffectiveDate { get; set; }
public DateTime ExpirationDate { get; set; }
public virtual Entity Entity{ get; set; }
public virtual Program Program { get; set; }
public virtual ICollection<Transaction> Transactions { get; set; }
}
为了使Dapper.Extensions 工作,我需要为这些类中的每一个编写一个映射,这很好。我的问题是,如果一个类中有任何virtual 属性,它们需要显式标记为ignored,而我总是忘记这样做。
public sealed class PolicyMapper : BaseMapper<Policy>
{
public PolicyMapper()
{
Map(p => p.Entity).Ignore();
Map(p => p.Program).Ignore();
Map(p => p.Transactions).Ignore();
AutoMap();
}
}
如果Dapper.Extensions 库在映射到 POCO 类时会自动排除虚拟属性(如果有的话),那对我有什么好处。 Automapper 有一个扩展,它做类似的事情 (link)。有没有办法为Dapper.Extensions 库做到这一点?可能是这样的:
public sealed class PolicyMapper : BaseMapper<Policy>
{
public PolicyMapper()
{
IgnoreAllVirtual();
AutoMap();
}
}
【问题讨论】:
标签: c# mapping dapper dapper-extensions