Decimal Precision and Scale in Ef Code First

Decimal precision and scale in EF Code First

The answer from Dave Van den Eynde is now out of date. There are 2 important changes, from EF 4.1 onwards the ModelBuilder class is now DbModelBuilder and there is now a DecimalPropertyConfiguration.HasPrecision Method which has a signature of:

public DecimalPropertyConfiguration HasPrecision(
byte precision,
byte scale )

where precision is the total number of digits the db will store, regardless of where the decimal point falls and scale is the number of decimal places it will store.

Therefore there is no need to iterate through properties as shown but the can just be called from

public class EFDbContext : DbContext
{
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Class>().Property(object => object.property).HasPrecision(12, 10);

base.OnModelCreating(modelBuilder);
}
}

Set decimal(16, 3) for a column in Code First Approach in EF4.3

The DataType Attribute is a Validation Attribute. You need to do that using the ModelBuilder.

public class MyContext : DbContext
{
public DbSet<MyClass> MyClass;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyClass>().Property(x => x.SnachCount).HasPrecision(16, 3);
modelBuilder.Entity<MyClass>().Property(x => x.MinimumStock).HasPrecision(16, 3);
modelBuilder.Entity<MyClass>().Property(x => x.MaximumStock).HasPrecision(16, 3);
}
}

How to change the precision and the scale of decimal globally through code first?

You can use DbModelBuilder.Properties method:

Begins configuration of a lightweight convention that applies to all primitive properties of the specified type in the model.

like this:

modelBuilder.Properties<decimal>().Configure(p => p.HasPrecision(12, 10));

Entity Framework 6 decimal precision

I had a similar problem in a project. Oddly, when I changed my model from decimal? to decimal it started working. I also found a configuration setting TruncateDecimalsToScale which can be added:

public class DbContextConfiguration : DbConfiguration
{
public DbContextConfiguration()
{
var providerInstance= SqlProviderServices.Instance;
SqlProviderServices.TruncateDecimalsToScale = false;
this.SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
}
}

Incorrect Decimal precision and scale when creating database with Code First in Entity Framework 6.1

I solved the problem. Happens that Visual Studio creates a class ApplicationDbContext as part of the project that already inherits from IdentityDbContext, so the MyModel class should inherits from this class or I should set the decimal properties in the OnModelCreating of ApplicationDbContext class:

public partial class MyModel : ApplicationDbContext
{
public MyModel()
: base("DefaultConnection")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyModel>());
}
//....other properties

public virtual DbSet<ProductClientDiscount> ProductClientPrice { get; set; }
public virtual DbSet<ClientCategoryDiscount> ClientCategoryDiscount { get; set; }
public virtual DbSet<Product> Products { get; set; }
//....other properties


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);


modelBuilder.Entity<ClientCategoryDiscount>().Property(x => x.PercentDiscountRent).HasPrecision(12, 9);
modelBuilder.Entity<ClientCategoryDiscount>().Property(x => x.PercentDiscountSale).HasPrecision(12, 9);
modelBuilder.Entity<ProductClientDiscount>().Property(x => x.PercentDiscountRent).HasPrecision(12, 9);
modelBuilder.Entity<ProductClientDiscount>().Property(x => x.PercentDiscountSale).HasPrecision(12, 9);
modelBuilder.Entity<Product>().Property(x => x.SalePrice).HasPrecision(12, 9);
modelBuilder.Entity<Product>().Property(x => x.RentPrice).HasPrecision(12, 9);

}
}

Both ways works fine!

Entityframework model first decimal precision

OK I feel a little silly for not figuring this out earlier, but this is a simple fix.

In the properties window for the column in the edmx designer, both precision and scale are a drop down menu, however it still lets you type in it. So I just typed 2 in for scale, hit enter, and it worked.

Not sure why they have it setup to be a drop down if they don't provide you with any preset values and expect you to type it in.

Entity Framework Core - setting the decimal precision and scale to all decimal properties

You got close. Here's the code.

foreach (var property in modelBuilder.Model.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?)))
{
// EF Core 1 & 2
property.Relational().ColumnType = "decimal(18, 6)";

// EF Core 3
//property.SetColumnType("decimal(18, 6)");

// EF Core 5
//property.SetPrecision(18);
//property.SetScale(6);
}
// EF Core 6
protected override void ConfigureConventions(
ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Properties<decimal>()
.HavePrecision(18, 6);
}


Related Topics



Leave a reply



Submit