Zahra Momeni
Zahra Momeni

Reputation: 53

Post a decimal value in database using ASP.NET Web API

I am working on an ASP.NET Web API project and I have a database that I am using, and the model that I am working on look something like this

public class PAY
{
    public int ID { get; set; }
    public string PAY_CODE { get; set; }
    public Nullable<decimal> PAY_CASH { get; set; }
}

and I have a simple post method look like this:

[HttpPost]
public string AddPayROW(string PAY_CODE, decimal PAY_CASH,)
{
    var PAY = new PAY()
            {
                PAY_CODE = PAY_CODE,
                PAY_CASH = PAY_CASH,
            };
    db.PAY.Add(PAY);
    db.SaveChanges();

    return "Row added successfully";
}

My problem is kind of weird but when I post this decimal value to the table the last number is zero

For example, if I tried to post 1.999 will be saved like this 1.990

I am using SQL Server and the datatype is decimal(16, 3)

How can I fix this problem?

Upvotes: 0

Views: 527

Answers (1)

cbalakus
cbalakus

Reputation: 630

On your OnModelCreating function add this code,

modelBuilder.Entity<PAY>().Property(x => x.PAY_CASH).HasPrecision(18,3);

Hope it works.

Edit : Do you have DbContext like this? For example;

public partial class DBO : DbContext
{
    public DBO()
        : base("ConStrEtc")
    { }
    public virtual DbSet<PAY> PAYs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<PAY>().Property(x => x.PAY_CASH).HasPrecision(18, 3);
    }
}

public class PAY
{
    public int ID { get; set; }
    public string PAY_CODE { get; set; }
    public Nullable<decimal> PAY_CASH { get; set; }
}

Upvotes: 1

Related Questions