. Net Entity Framework Core uses HasColumnType to configure the floating point precision,
I. Preface
Some time ago, we used. Net Entity Framework core to build a Framework. We needed to configure the floating point precision. We found that. Net Entity Framework core does not have the HasPrecision method. There are few materials available on the Internet. Finally, the official documentation explains how to use HasColumnType to configure the floating point precision.
Ii. HasColumnType official documentation
Document connection:
Https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.entityframeworkcore.relationalpropertybuilderextensions.hascolumntype? View = efcore-2.0 # Empty _
Iii. Comparison of. Net Entity Framework and. Net Entity Framework Core Configuration
. Net Entity Framework method:
Public class MyProjectContext : DbContext
{
Public DbSet<Order> Orders { get; set; }
Protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
/ / Configure the amount of the order floating point precision is decimal (18,6)
modelBuilder.Entity<Order>().Property(t => t.Amount).HasPrecision(18, 6);
}
}
.Net Entity Framework Core method:
Public class MyProjectContext : DbContext
{
Public DbSet<Order> Orders { get; set; }
Protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Order>(b =>
{
/ / Configure the amount of the order floating point precision is decimal (18,6)
b.Property(p => p.Amount).HasColumnType("decimal(18,6)");
});
}
}