自动映射器向下舍入十进制/删除精度



我正在将实体模型映射到UI模型,但是在映射完成后,几个字段将向下舍入。所有受影响的字段都是相同的十进制类型?,从数据库中提取时,字段为 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");
}
}

我假设这是自动映射器中的东西?当实体模型被拉出时,值是正确的,然后我映射它,模型中的值现在向下舍入。

可能还值得一提的是,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>();

干杯

试试这个:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<NameOfEntity>()
.Property(p => p.NameOfProperty)
.HasPrecision(9, 4); // or whatever your schema specifies
}

最新更新