c# - 验证 30000 没有为小数列指定类型

标签 c# entity-framework ef-core-2.0

在不使用属性的情况下指定小数精度的最佳方法是什么。
我只需要在我的 Data.Models 中将所有小数设置在一个位置。其繁琐的为每个小数指定属性。

public class Customer
{
    public int customerId{ get; set; }

    [Column(TypeName = "decimal(18,2)")]
    public decimal AvailableAmount{ get; set; }
}

最佳答案

将以下内容添加到 OnModelCreating dbcontext 中的方法:

 protected override void OnModelCreating(ModelBuilder builder)
        {
          
            foreach (var property in builder.Model.GetEntityTypes()
                .SelectMany(t => t.GetProperties())
                .Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?)))
            {
              
                property.Relational().ColumnType = "decimal(18,2)";

              
            }
        }

关于c# - 验证 30000 没有为小数列指定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62550667/

相关文章:

c# - 'CustomerId' 操作的结果中不存在所需的列 'FromSql'

c# - Entity Framework 4 到 6 的解决方法

entity-framework-core - EF Core - 为什么 ClientSetNull 是可选关系的默认 OnDelete 行为(而不是 SetNull)

asp.net-core - Microsoft.EntityFrameworkCore.Sqlite.Core和Microsoft.EntityFrameworkCore.Sqlite程序集之间有什么区别?

c# - 将 Excel 转换为 xml

c# - 来自 IHttpClientFactory 的类型化 HttpClient 实例的生命周期是多长,其中将接收它的类型注册为 "AddScoped"?

C# LINQ分组和计数问题

c# - EntityFramework - 选择具有自定义属性投影的实体

c# - 取消订阅处理程序中的事件总是安全的吗?

c# - EF core QueryFilter 的性能问题