I'm using the Entity framework (6.1.3) (database first) for one of my projects where I have three tables in the database which contains decimal fields.
The decimal fields are set with a precision of 18 and a scale of 5, like this: decimal(18, 5)
I have checked the edmx XML file and it seems like the decimals are mapped as they should. I.e.:
<Property Name="Price" Type="decimal" Precision="18" Scale="5" Nullable="false" />
However, when I try to save a calculated decimal value which looks like, i.e. 28021.800000000000000000000002
, I get an exception telling me that the property value is out of range.
Shouldn't entity framework just save the precision and scaled value of this decimal? If not, how exactly do I make my decimal "valid"?
Thanks in advance :-)
Edited with code sample:
// 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;