C# 如何使用Structuremap设置Dapper扩展自定义映射?
我正在使用Dapper扩展在配置为使用Structuremap的MVC应用程序中构建我的存储库。对于其中一个模型,我需要创建一个自定义映射来忽略一个字段C# 如何使用Structuremap设置Dapper扩展自定义映射?,c#,structuremap,dapper-extensions,C#,Structuremap,Dapper Extensions,我正在使用Dapper扩展在配置为使用Structuremap的MVC应用程序中构建我的存储库。对于其中一个模型,我需要创建一个自定义映射来忽略一个字段 public class ServiceMapper : ClassMapper<Service> { public ServiceMapper() { //Ignore this property entirely Map(x => x.IsRunningNormally).I
public class ServiceMapper : ClassMapper<Service>
{
public ServiceMapper()
{
//Ignore this property entirely
Map(x => x.IsRunningNormally).Ignore();
//optional, map all other columns
AutoMap();
}
}
我一碰到这一行,Structuremap就会尝试解析该类型并抛出异常:
ServiceMonitor.Infrastructure.ServiceMapper不是GenericTypeDefinition。MakeGenericType只能在type.IsGenericTypeDefinition为true的类型上调用
我不知道这个错误是什么意思以及如何解决它?有谁能告诉我这里发生了什么事吗?好的,所以终于解决了问题。问题是默认情况下,DapperExtensions将在与模型POCO类相同的程序集中扫描您编写的任何自定义映射程序。在我的例子中,是DataTransferObjects程序集 我的映射器类存在于与DTOs程序集不同的存储库程序集中 我需要告诉Dapper Extensions扫描此程序集以查找自定义映射:
DapperExtensions.DapperExtensions.DefaultMapper = typeof (ServiceMapper);
// Tell Dapper Extension to scan this assembly for custom mappings
DapperExtensions.DapperExtensions.SetMappingAssemblies(new[]
{
typeof (ServiceMapper).Assembly
});
一旦我像上面那样设置,我的代码就开始工作了。这在任何地方都没有记录,我花了一段时间才弄明白。希望它能帮助其他有同样问题的人。请注意,如果您使用的是异步IMNPlements,则需要使用DapperAsyncExtensions注册映射程序集:
DapperExtensions.DapperAsyncExtensions.DefaultMapper = typeof (ServiceMapper);
// Tell Dapper Extension to scan this assembly for custom mappings
DapperExtensions.DapperAsyncExtensions.SetMappingAssemblies(new[]
{
typeof (ServiceMapper).Assembly
});
DapperExtensions.DapperAsyncExtensions.DefaultMapper = typeof (ServiceMapper);
// Tell Dapper Extension to scan this assembly for custom mappings
DapperExtensions.DapperAsyncExtensions.SetMappingAssemblies(new[]
{
typeof (ServiceMapper).Assembly
});