Description
Hi guys.
I had a problem with an project with Database First Approach. My app was truncating every decimal's information.
Consider model as:
class Sell
{
public int SellerId { get; set; }
public decimal Total { get; set; }
}
and Context as:
// This context was generated by 'dotnet ef scaffold' command
class MainContext
{
public DbSet<Sell> Sells { get; set; }
modelBuilder.Entity<Sell>(entity =>
{
entity.Property(e => e.Total)
.HasColumnType("DECIMAL")
.HasColumnName("TOTAL");
}
}
When I perform an insert/update operation, it just saves the integer part of decimal.
var sell = new Sell {
SellerId = 1.
Total = 323.99
};
context.Sells.Add(sell);
await context.SaveChangesAsync(); // It just saved 323
Looking for a solution I realized the problem was column type generated by ef scaffolding proccess. I adjusted my context as follows:
// This context was generated by 'dotnet ef scaffold' command
class MainContext
{
public DbSet<Sell> Sells { get; set; }
modelBuilder.Entity<Sell>(entity =>
{
entity.Property(e => e.Total)
.HasColumnType("NUMERIC(10, 3)")
.HasColumnName("TOTAL");
// I changed DECIMAL to NUMERIC with precision present on table's field.
}
}
I would like to ask to fix this issue and I give my problem and how I solved this problem.
Thanks in advance.