1、前言api
前段時間用.Net Entity Framework core搭建框架,須要配置浮點數的精度,發現.Net Entity Framework core 並無HasPrecision方法。在網上查找資料也比較少,最後經過官方文檔說明,嘗試使用HasColumnType配置浮點數精度成功。框架
2、HasColumnType官方文檔說明ide
文檔鏈接:ui
https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.entityframeworkcore.relationalpropertybuilderextensions.hascolumntype?view=efcore-2.0#Microsoft_EntityFrameworkCore_RelationalPropertyBuilderExtensions_HasColumnType_Microsoft_EntityFrameworkCore_Metadata_Builders_PropertyBuilder_System_String_spa
3、對比.Net Entity Framework 和.Net Entity Framework Core 配置code
.Net Entity Framework方法:blog
public class MyProjectContext : DbContext { public DbSet<Order> Orders { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder);
//配置訂單的金額浮點數精度爲decimal(18,6) modelBuilder.Entity<Order>().Property(t => t.Amount).HasPrecision(18, 6); } }
.Net Entity Framework Core方法:ci
public class MyProjectContext : DbContext { public DbSet<Order> Orders { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Order>(b => { //配置訂單的金額浮點數精度爲decimal(18,6) b.Property(p => p.Amount).HasColumnType("decimal(18,6)"); }); } }