I'm using Entity Framework 6.1, code-first. I'm trying to store/retrieve a decimal from SQL Server, but the correct precision isn't retrieved. I read from a .csv file, process the data, then insert into the DB.
1.1390
decimal
from the import: 1.1390
1.139
1.1390
1.1390
1.1300
Everything is working except for retrieving the value. I've tried the solution given in every single other SO post (below), but it has no effect. What's going wrong here? How can I retrieve the correct value with the proper precision?
Supposed solution:
modelBuilder.Entity<ClassA>().Property(p => p.MyDecimal).HasPrecision(19, 4);
Retrieval method, returns type ClassA
with incorrect precision:
_poRepository.Table.FirstOrDefault(p => p.CompNumberA == compNum &&
p.LineNumber == lineNumber);
Comparison between the two decimal values:
import.MyDecimal != dbValue.MyDecimal
ClassA
:
public class ClassA
{
// properties
public virtual decimal MyDecimal { get; set; }
}
Turns out I needed to specify the precision the same way, but in the fluent mapping for ClassA
. Specifying it in OnModelCreating()
didn't do it, but in the fluent mapping did. Specifically:
Property(u => u.MyDecimal).HasPrecision(19, 4);
In Entity Framework Core you can do:
[Column(TypeName = "decimal(19,4)")]
public decimal MyDecimal { get; set; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With