If you have used EF core, you have seen that there is no HasPrecision method to use and all of the decimal data types will be map as decimal(18,2) in SQL server by default. We used to map decimal numbers in a format rather than default like below:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<MyEntity>() .Property(p => p.MyProperty) .HasPrecision(10, 7); }
I recently wrote a custom extension method to use in EF Core:
public static class SqlServerModelBuilderExtensions { public static PropertyBuilder<decimal?> HasPrecision(this PropertyBuilder<decimal?> builder, int precision, int scale) { return builder.HasColumnType($"decimal({precision},{scale})"); } public static PropertyBuilder<decimal> HasPrecision(this PropertyBuilder<decimal> builder, int precision, int scale) { return builder.HasColumnType($"decimal({precision},{scale})"); } }
By using this, you can easily map your custom decimal data type into another Precision.
Category: Software
Tags: Entity Framework