Use money type in Entity Framework model first

Viewed 19776

I created a table that has a column called Amount and I set it as Decimal. I can't find where to set it as money or smallmoney and when I try to set a value like 0.2, it rounds to 0....

How can I use money with entity framework?

thanks!

4 Answers

My response is regarding E.F 6.0 After dealing with a similar issue and check with SQL Profiler: Entity framework translates the Decimal variable type as Decimal(18, 2) in SQL.

If you want save Money type I would recommend to use Data Annotations and add

public class Account
{
    public int AccountId { get; set; }                

    [Column(TypeName="money")]
    public decimal Amount { get; set; }
}

Or if you use Fluent API

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Account>()
                .Property(i => i.Amount)
                .HasColumnType("money");
}
  • Money type will translated to (19, 4)
Related