【发布时间】:2020-02-13 13:05:53
【问题描述】:
我正在将实体模型映射到 UI 模型,但是在映射完成后,几个字段正在向下舍入。所有受影响的字段都是相同的类型 decimal? ,当从数据库中提取它时,该字段是 8245.82 但是一旦映射它是 8245.00
public IActionResult View(Guid Id)
{
try
{
DealEx entity = _dealService.Get(m => m.Id == Id);
var model = _mapper.Map<DealUI>(entity);
AddReferenceData(model);
return PartialView("_deal", model);
}
catch (Exception ex)
{
this.ProcessException(ex, _logger);
return View("Error");
}
}
我假设这是 Automapper 中的内容?当 entity 模型被拉出时,值是正确的,然后我对其进行映射,并且 model 中的值现在向下舍入。
可能还值得一提的是 DealUI 继承了 DealEx 。
public class DealUI : DealEx
{
public DealUI()
{
}
// just some select lists in here , northing of interest to this
}
public class DealEx
{
public DealEx(){}
public decimal? BillValue {get; set;}
// obivously alot more in this model but it's a big ole model so copying it all seems a bit pointless
}
包括映射要简洁
CreateMap<DealEx, DealUI>();
CreateMap<DealUI, DealEx>();
干杯
【问题讨论】:
-
复制会有所帮助。创建一个gist,我们可以执行并看到失败。
-
添加 DealEx 、 DealUI 和映射器会有所帮助。
-
对不起,我第一次误解了你的问题,我现在添加了。干杯
标签: c# asp.net .net automapper