我正在使用实体框架(6.1.3)(首先是 数据库 )用于我的一个项目,其中我在数据库中有三个表十进制字段。
十进制字段的精度为18,比例为5,如下所示:decimal(18, 5)
我检查了edmx XML文件,似乎小数位被映射为应有的。即:
<Property Name="Price" Type="decimal" Precision="18" Scale="5" Nullable="false" />
但是,当我尝试保存看起来像28021.800000000000000000000002
的计算十进制值时,我得到一个异常,告诉我属性值超出范围。
不应该实体框架只保存此小数的精度和缩放值吗?如果没有,我究竟如何制作我的小数&#34;有效&#34;?
提前致谢: - )
使用代码示例编辑:
// Loop through billing price lines on this billing
foreach (BillingPriceLine priceLine in billing.BillingPriceLines)
{
// Price line specification sum fields
decimal totalPriceLineSpecificationProduction = 0;
decimal totalPriceLineSpecificationSum = 0;
// Loop through billing price line specifications on this price line
foreach (BillingPriceLineSpecification specification in priceLine.BillingPriceLineSpecifications)
{
// First, check if the estimated production and realised production has a value
if (specification.EstimatedProduction.HasValue && specification.RealisedProduction.HasValue)
{
// Calculate production for a price line specification
specification.Production = specification.EstimatedProduction.Value - specification.RealisedProduction.Value;
// Add production to total price line specification production
totalPriceLineSpecificationProduction = specification.Production;
// Add to total price line specification sum
totalPriceLineSpecificationSum += specification.Production * specification.Price;
}
}
// Set total production on price line
priceLine.Production = totalPriceLineSpecificationSum;
// Set price on price line
priceLine.Price = totalPriceLineSpecificationProduction / priceLine.Production;
// Set total price on price line
priceLine.TotalPrice = (priceLine.Production * priceLine.Price) * 100;
}
// Set subtotal, VAT and total sum on billing
billing.Subtotal = billing.BillingPriceLines.Sum(x => x.TotalPrice);
billing.VAT = billing.Subtotal / 4;
billing.Total = billing.Subtotal + billing.VAT;
// Save changes in database
return ctx.SaveChanges() > 0;