EF retrieves invalid decimal precision

I am using Entity Framework 6.1, code first. I am trying to store / retrieve a decimal from SQL Server, but the correct precision is not retrieved. I read from a CSV file, processed the data, and then inserted it into the database.

  • Value in the CSV file: 1.1390
  • Value when saving as decimal from import: 1.1390
  • The value inserted into the database: 1.139
  • Value actually saved / displayed in SSMS: 1.1390
  • The value when you manually run the query SQL Profiler commits to extract: 1.1390
  • Value Received by EF: 1.1300

Everything works, except for getting the value. I tried the solution given in all other SO posts (below), but this has no effect. What's going on here? How can I get the right value with the right precision?

Estimated Solution:

 modelBuilder.Entity<ClassA>().Property(p => p.MyDecimal).HasPrecision(19, 4); 

The search method returns a ClassA type with the wrong precision:

 _poRepository.Table.FirstOrDefault(p => p.CompNumberA == compNum && p.LineNumber == lineNumber); 

Comparison of two decimal values:

 import.MyDecimal != dbValue.MyDecimal 

ClassA :

 public class ClassA { // properties public virtual decimal MyDecimal { get; set; } } 
+8
c # entity-framework
source share
1 answer

Turns out I need to specify the precision exactly the same way, but in the current mapping for ClassA . Specifying it in OnModelCreating() did not do this, but in a free display. In particular:

 Property(u => u.MyDecimal).HasPrecision(19, 4); 
+3
source share

All Articles