EF Code First中的十进制精度和比例

Dav*_*nde 215 .net c# entity-framework decimal ef-code-first

我正在尝试这种代码优先的方法,但我现在发现System.Decimal类型的属性被映射到decimal(18,0)类型的sql列.

如何设置数据库列的精度?

Ale*_*exC 245

Dave Van den Eynde的答案现已过时.有两个重要的变化,从EF 4.1开始,ModelBuilder类现在是DbModelBuilder,现在有一个DecimalPropertyConfiguration.HasPrecision方法,其签名为:

public DecimalPropertyConfiguration HasPrecision(
byte precision,
byte scale )
Run Code Online (Sandbox Code Playgroud)

其中precision是db将存储的总位数,无论小数点落在何处,scale是它将存储的小数位数.

因此,不需要迭代所示的属性,但可以从中调用

public class EFDbContext : DbContext
{
   protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
   {
       modelBuilder.Entity<Class>().Property(object => object.property).HasPrecision(12, 10);

       base.OnModelCreating(modelBuilder);
   }
}
Run Code Online (Sandbox Code Playgroud)

  • HasPrecision的两个参数(精度,比例)记录很少.precision是它将存储的总位数,无论小数点落在何处.scale是它将存储的小数位数. (22认同)
  • 是否有一个 EF 配置可以在一处为所有实体上的所有十进制属性设置它?我们一般使用 (19,4) 。如果能自动将其应用于所有十进制属性,那就太好了,这样我们就不会忘记设置属性精度而错过计算中的预期精度。 (2认同)

kjb*_*tel 83

如果要decimals在EF6中为all设置精度,可以替换以下中DecimalPropertyConvention使用的默认约定DbModelBuilder:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<DecimalPropertyConvention>();
    modelBuilder.Conventions.Add(new DecimalPropertyConvention(38, 18));
}
Run Code Online (Sandbox Code Playgroud)

DecimalPropertyConventionEF6中的默认值将decimal属性映射到decimal(18,2)列.

如果您只希望单个属性具有指定的精度,则可以在以下位置设置实体属性的精度DbModelBuilder:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<MyEntity>().Property(e => e.Value).HasPrecision(38, 18);
}
Run Code Online (Sandbox Code Playgroud)

或者,EntityTypeConfiguration<>为指定精度的实体添加一个:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new MyEntityConfiguration());
}

internal class MyEntityConfiguration : EntityTypeConfiguration<MyEntity>
{
    internal MyEntityConfiguration()
    {
        this.Property(e => e.Value).HasPrecision(38, 18);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我最喜欢的解决方案。使用 CodeFirst 和迁移时完美运行:EF 在所有使用“十进制”的类中查找所有属性,并为这些属性生成迁移。伟大的! (2认同)

Kin*_*rUY 70

我很高兴为此创建自定义属性:

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class DecimalPrecisionAttribute : Attribute
{
    public DecimalPrecisionAttribute(byte precision, byte scale)
    {
        Precision = precision;
        Scale = scale;

    }

    public byte Precision { get; set; }
    public byte Scale { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

像这样使用它

[DecimalPrecision(20,10)]
public Nullable<decimal> DeliveryPrice { get; set; }
Run Code Online (Sandbox Code Playgroud)

并且通过一些反思在模型创造中发生了魔力

protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
    foreach (Type classType in from t in Assembly.GetAssembly(typeof(DecimalPrecisionAttribute)).GetTypes()
                                   where t.IsClass && t.Namespace == "YOURMODELNAMESPACE"
                                   select t)
     {
         foreach (var propAttr in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<DecimalPrecisionAttribute>() != null).Select(
                p => new { prop = p, attr = p.GetCustomAttribute<DecimalPrecisionAttribute>(true) }))
         {

             var entityConfig = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(classType).Invoke(modelBuilder, null);
             ParameterExpression param = ParameterExpression.Parameter(classType, "c");
             Expression property = Expression.Property(param, propAttr.prop.Name);
             LambdaExpression lambdaExpression = Expression.Lambda(property, true,
                                                                      new ParameterExpression[]
                                                                          {param});
             DecimalPropertyConfiguration decimalConfig;
             if (propAttr.prop.PropertyType.IsGenericType && propAttr.prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
             {
                 MethodInfo methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[7];
                 decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
             }
             else
             {
                 MethodInfo methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[6];
                 decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
             }

             decimalConfig.HasPrecision(propAttr.attr.Precision, propAttr.attr.Scale);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

第一部分是获取模型中的所有类(我的自定义属性在该程序集中定义,所以我用它来获取模型的程序集)

第二个foreach使用自定义属性和属性本身获取该类中的所有属性,以便我可以获得精度和比例数据

之后我必须打电话

modelBuilder.Entity<MODEL_CLASS>().Property(c=> c.PROPERTY_NAME).HasPrecision(PRECISION,SCALE);
Run Code Online (Sandbox Code Playgroud)

所以我通过反射调用modelBuilder.Entity()并将其存储在entityConfig变量中然后构建"c => c.PROPERTY_NAME"lambda表达式

之后,如果小数可以为空,我会调用

Property(Expression<Func<TStructuralType, decimal?>> propertyExpression) 
Run Code Online (Sandbox Code Playgroud)

方法(我通过数组中的位置调用它,它不是理想我知道,任何帮助将不胜感激)

如果它不可空,我会打电话给

Property(Expression<Func<TStructuralType, decimal>> propertyExpression)
Run Code Online (Sandbox Code Playgroud)

方法.

使用DecimalPropertyConfiguration我调用HasPrecision方法.

  • 谢谢你.它使我免于生成数以千计的lambda表达式. (3认同)
  • 我把它包装成一个库,让它更容易从DbContext调用:https://github.com/richardlawley/EntityFrameworkAttributeConfig(也可以通过nuget获得) (3认同)
  • 我使用`MethodInfo methodInfo = entityConfig.GetType().GetMethod("Property",new [] {lambdaExpression.GetType()});`获取正确的重载.似乎到目前为止工作. (2认同)

Dav*_*nde 47

显然,您可以覆盖DbContext.OnModelCreating()方法并配置如下精度:

protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Product>().Property(product => product.Price).Precision = 10;
    modelBuilder.Entity<Product>().Property(product => product.Price).Scale = 2;
}
Run Code Online (Sandbox Code Playgroud)

但是当你必须使用所有与价格相关的属性时,这是相当繁琐的代码,所以我想出了这个:

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    {
        var properties = new[]
        {
            modelBuilder.Entity<Product>().Property(product => product.Price),
            modelBuilder.Entity<Order>().Property(order => order.OrderTotal),
            modelBuilder.Entity<OrderDetail>().Property(detail => detail.Total),
            modelBuilder.Entity<Option>().Property(option => option.Price)
        };

        properties.ToList().ForEach(property =>
        {
            property.Precision = 10;
            property.Scale = 2;
        });

        base.OnModelCreating(modelBuilder);
    }
Run Code Online (Sandbox Code Playgroud)

在覆盖方法时调用基本方法是一种很好的做法,即使基本实现不执行任何操作也是如此.

更新:这篇文章也很有帮助.

  • 谢谢,这指向了我正确的方向.在CTP5中,语法已更改为允许在同一语句中添加Precision和Scale:modelBuilder.Entity <Product>().Property(product => product.Price).HasPrecision(6,2); (10认同)
  • 我不认为调用`base.OnModelCreating(modelBuilder);`是必要的.来自VS中的DbContext元数据:`此方法的默认实现不执行任何操作,但可以在派生类中重写它,以便在锁定模型之前可以进一步配置模型. (3认同)
  • 不过,为*all*小数设置某种"默认"会不会很好? (2认同)

kjb*_*tel 47

使用DecimalPrecisonAttributefrom KinSlayerUY,在EF6中,您可以创建一个约定来处理具有该属性的各个属性(而不是DecimalPropertyConvention此答案中设置类似于将影响所有小数属性的属性).

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class DecimalPrecisionAttribute : Attribute
{
    public DecimalPrecisionAttribute(byte precision, byte scale)
    {
        Precision = precision;
        Scale = scale;
    }
    public byte Precision { get; set; }
    public byte Scale { get; set; }
}

public class DecimalPrecisionAttributeConvention
    : PrimitivePropertyAttributeConfigurationConvention<DecimalPrecisionAttribute>
{
    public override void Apply(ConventionPrimitivePropertyConfiguration configuration, DecimalPrecisionAttribute attribute)
    {
        if (attribute.Precision < 1 || attribute.Precision > 38)
        {
            throw new InvalidOperationException("Precision must be between 1 and 38.");
        }

        if (attribute.Scale > attribute.Precision)
        {
            throw new InvalidOperationException("Scale must be between 0 and the Precision value.");
        }

        configuration.HasPrecision(attribute.Precision, attribute.Scale);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());
}
Run Code Online (Sandbox Code Playgroud)

  • @kjbartel确实有些数据库提供程序支持大于28的精度; 但是,根据MSDN,`System.Decimal`没有.因此,将上限前置条件设置为大于28的任何值都没有意义; `System.Decimal`显然不能代表那么大的数字.另请注意,此属性对SQL Server以外的数据提供程序很有用.例如,PostgreSQL的`numeric`类型支持最多131072位精度. (2认同)

mxa*_*sim 30

实体框架Ver 6(Alpha,rc1)具有称为自定义约定的东西.要设置小数精度:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Properties<decimal>().Configure(config => config.HasPrecision(18, 4));
}
Run Code Online (Sandbox Code Playgroud)

参考:


小智 25

从 .NET EF Core 6 开始,您可以使用 Precision 属性。

    [Precision(18, 2)]
    public decimal Price { get; set; }
Run Code Online (Sandbox Code Playgroud)

确保您需要安装 EF Core 6 并执行以下using操作

using Microsoft.EntityFrameworkCore;
Run Code Online (Sandbox Code Playgroud)


Eln*_*oor 16

[Column(TypeName = "decimal(18,2)")]
Run Code Online (Sandbox Code Playgroud)

这将与此处描述的代码首次迁移一起使用.

  • 如果你只是将其添加到你的模型中,你会得到“在 SqlServer 提供程序清单中找不到存储类型 'decimal(18,2)'” (3认同)

arm*_*.mx 14

这段代码可能是一种更简单的方法来实现相同的:

 public class ProductConfiguration : EntityTypeConfiguration<Product>
    {
        public ProductConfiguration()
        {
            this.Property(m => m.Price).HasPrecision(10, 2);
        }
    }
Run Code Online (Sandbox Code Playgroud)


sof*_*ntp 7

- EF的核心-使用System.ComponentModel.DataAnnotations;

使用 [ColumnTypeName = "decimal精度刻度")]

精度 = 使用的字符总数

比例 = 点后的总数。(容易混淆)

范例

public class Blog
{
    public int BlogId { get; set; }
    [Column(TypeName = "varchar(200)")]
    public string Url { get; set; }
    [Column(TypeName = "decimal(5, 2)")]
    public decimal Rating { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

此处有更多详细信息:https : //docs.microsoft.com/zh-cn/ef/core/modeling/relational/data-types