【发布时间】:2014-05-31 18:23:59
【问题描述】:
我正在尝试使用 Automapper 'Project' 方法在 EF 域对象到 DTO 对象之间执行映射,但是在尝试从接口投影到具体类时遇到问题。我的 EF 域对象实现了一个我在查找表中常用的接口:
public interface ILookupItem
{
int Id { get; set; }
string Name { get; set; }
}
这是我的域对象的示例:
public partial class ReportType : ILookupItem
{
public int Id { get; set; }
public string Name { get; set; }
}
在我的应用程序中,我使用了与域对象接口完全匹配的 DTO 对象:
public class LookupItemModel
{
public static void CreateMapping(IConfiguration configuration)
{
configuration.CreateMap<ILookupItem, LookupItemModel>();
}
public int Id { get; set; }
public string Name { get; set; }
}
然后我通过如下调用执行我的数据库查询:
return DbContext.Query<ReportType>().Project().To<LookupItemModel>();
然而,在这个调用中,Automapper 给出了一个关于缺少执行该功能所需的映射的错误:
Missing map from ReportType to LookupItemModel. Create using Mapper.CreateMap<ReportType, LookupItemModel>.
我会假设映射可以从接口执行,因为它只需要知道为(ID 和名称)提取数据的属性。我是否遗漏了一些能够在不为我的接口的每个具体实现创建地图的情况下执行此投影的东西?
谢谢!
【问题讨论】:
-
您是否曾经在 LookupItemModel 上调用过 CreateMapping()?
标签: c# entity-framework automapper