.NET Core model is rounding my decimal values

540 Views Asked by At

I have a .NET Core 2.0 app that is pulling data from an API and trying to save it to a table cog_detail (using the model CogDetail)

foreach (Models.Cog.CogDetail cd in cdList)
{
   _context.CogDetail.Add(cd);
   _context.SaveChanges();
}

One of the values is called Lmn and shows as 2.60895M when I check the value in cd (I have a breakpoint on _context.CogDetail.Add(cd);)

But once it does the save, it is saving in the database as 3

The SQL Server table has the column as decimal(18, 0), null

My context has this:

modelBuilder.Entity<Models.Cog.CogDetail>(entity =>
{
  entity.HasKey(e => new { e.Test_ID, e.TCode }).HasName("PK_cog_detail");

  entity.ToTable("cog_detail", "cog");

  entity.Property(e => e.Test_ID)
    .HasColumnName("test_id")
    .HasColumnType("int");

  entity.Property(e => e.TCode)
    .HasColumnName("tcode")
    .HasColumnType("varchar(10)");

  entity.Property(e => e.Lmn)
    .HasColumnName("lmn")
    .HasColumnType("decimal(18, 0)");

});

Any ideas on why it is rounding up instead of staying as 2.60895?

1

There are 1 best solutions below

1
Kevin M On

Answer provided by Flydog57 and Train in the comments.

Column type should have been 18, 6 - not 18, 0 as that had no numbers to the right of the decimal point