C# Tips

C# Tip Article

How to change default precision and scale for decimal type in EF5

In Entity Frame 5, default precision for decimal property is (18, 2) which means precision is 18 and scale is 2.

If you want to change precision and scale for the decimal type, you will need to define it in DbContext.OnModelCreating() method as below.

For example, if Amount property of Order class has (18, 10) precision, specify the custom precision in HasPrecision() method.

public class MyDbContext : DbContext
{
    //... elided ...
	
	protected override void OnModelCreating(DbModelBuilder modelBuilder)
	{
		modelBuilder.Entity().Property(p => p.Amount).HasPrecision(18, 10);            

		base.OnModelCreating(modelBuilder);
	}
}	

public class Order
{
    //... elided ...
	
	public decimal Amount;
}