SQL Server Decimal - Why is my decimal value only capturing two decimal places after the decimal? Yes, it's decimal(18,5)

c#, sql, sql-server

Solution

I had a similar problem, so maybe this helps: I had to specify the type in the `DbContext`. In my case, the field in the database was of type `Money`, and the C# type for the models property was `Decimal`:

modelBuilder.Entity<MyEntity>().Property(e => e.Value).HasColumnType("Money");

Don't know, but maybe it works if you change that to "Decimal" or even "Decimal(18,5)" if that's possible. Or to pick up the comment of ta.speot:

modelBuilder.Entity<MyEntity>().Property(e => e.Value)
            .HasColumnType("Decimal")
            .HasPrecision(18, 5);

Problem

I'll just start by saying I have my decimal data type defined as decimal(18,5): The values coming from my app before it hits the database are: So as you can see so far, everything looks good. I should see a value of 0.47588 in my database table for column `Capsule105`. But, after my record saves to the database, I look at the record it inserted- take a look at the last row: The value shouldn't stop after two decimal places. It should go to the very last decimal place defined in my database, right? Here's my `InsertOrUpdate` method (came stock with the template in VS): ``` public void InsertOrUpdate(CalculatedResults calculatedresults) { if (calculatedresults.Pk == default(int)) { // New entity context.CalculatedResults.Add(calculatedresults); } else { // Existing entity context.Entry(calculatedresults).State = EntityState.Modified; } } ``` I'm using Code First approach and generating my database from my models. Any ideas what's going on here??? Please help!!

Original source

Related problems