ale*_*dru 2 c# automapper automapper-3
嗨,我正在尝试将AutoMapper添加到我的应用程序中,但我似乎在eintegrationg中遇到了一些问题.这是我到目前为止所拥有的.
为了不创建与Automapper的直接依赖关系,我为它的最基本功能创建了一个简单的maping:
public class AutoMapper : IAutoMapper
{
public void CreateMap<TFrom, TTo>()
{
Mapper.CreateMap<TFrom, TTo>();
}
public TTo Map<TFrom, TTo>(TFrom data)
{
return Mapper.Map<TFrom, TTo>(data);
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个配置文件:
public class AutoMapperConfig
{
private readonly IAutoMapper mapper;
public AutoMapperConfig(IAutoMapper mapper)
{
this.mapper = mapper;
}
public void RegisterMappings()
{
mapper.CreateMap<ProductDTO , ProductDataContract>();
}
}
Run Code Online (Sandbox Code Playgroud)
并在我的Global.Asax中添加了一个调用:
new AutoMapperConfig(new AutoMapper()).RegisterMappings();
Run Code Online (Sandbox Code Playgroud)
我有这两个对象beetwen我想创建一个映射:
public class ProductDTO
{
public int ProductId { get; set; }
public int CategoryId { get; set; }
public int SubcategoryId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
public string ImagePath { get; set; }
public int NumberOfProducts { get; set; }
}
public class ProductDataContract
{
public int ProductId { get; set; }
public int CategoryId { get; set; }
public int SubcategoryId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
public string ImagePath { get; set; }
public int NumberOfProducts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我将此行放在测试目的中:
var products = productCatalogService.GetProducts(); ProductDTO ceva = products.FirstOrDefault(); var productsDataContract = mapper.Map(ceva);
问题是,当我运行我的应用程序时,我在尝试使用CreateMap时,在Automapper中获得异常.这是类型初始化异常消息:
此平台IDictionaryFactory不支持此类型
我究竟做错了什么?
| 归档时间: |
|
| 查看次数: |
1374 次 |
| 最近记录: |