.Net Entity Framework Core 用 HasColumnType 配置浮点数精度

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)");
            });   
     }   
}
相关文章
相关标签/搜索