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

287

我正在尝试使用代码优先的方法,但是现在发现一个类型为System.Decimal的属性被映射到了一个sql列的decimal(18, 0)类型中。

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


23
一种方法是在你的小数属性上使用[Column(TypeName = "decimal(18,4)")]属性。 - S.Serpooshan
1
[Column(TypeName = "decimal(18,4)")] 很棒!!! - Brian Rice
19个回答

310

Dave Van den Eynde 的回答现在已过时。从 EF 4.1 开始,ModelBuilder 类现在是 DbModelBuilder,并且现在有一个 DecimalPropertyConfiguration.HasPrecision 方法,其签名为:

public DecimalPropertyConfiguration HasPrecision(
byte precision,
byte scale )

精度(precision)是数据库将存储的数字总位数,无论小数点在哪里,而刻度(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);
   }
}

1
@BenSwayne 感谢你的提醒,这是我的疏忽,没有任何恶意。我会编辑答案。 - AlexC
45
HasPrecision(precision, scale)函数的两个参数文档不够清晰。precision表示它将存储的数字总位数,无论小数点在哪里。scale表示它将存储的小数点位数。 - Chris Moschini
2
是否有一个 EF 配置可以在一个地方为所有实体的所有十进制属性设置它?我们通常使用 (19,4)。如果所有十进制属性都自动应用此精度,那将是很好的,这样我们就不会忘记设置属性精度并在计算中错过预期的精度。 - Mike de Klerk
1
Property(object => object.property) 中,将 object 改为 x 以使其编译通过。 - Savage
1
在EF5中,我尝试使用.HasColumnType()HasPrecision(),但警告仍然显示在日志文件中。我该如何消除这个警告? - Peet Brits
显示剩余8条评论

105

如果您想为EF6中的所有小数设置精度,则可以替换DbModelBuilder中使用的默认DecimalPropertyConvention约定:

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

在EF6中,默认的DecimalPropertyConventiondecimal属性映射到decimal(18,2)列。

如果您只想让某些属性具有指定的精度,则可以在DbModelBuilder上设置实体属性的精度:

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

或者,为实体添加一个指定精度的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);
    }
}

3
我最喜欢的解决方案。在使用 CodeFirst 和迁移时,这种方法非常完美:EF 会查找所有类中使用了“decimal”的属性,并为这些属性生成迁移。太好了! - okieh

82
我很高兴为此创建了一个自定义属性:

我很享受为这个做出自定义属性的过程:

[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; }

}

使用如下方式

[DecimalPrecision(20,10)]
public Nullable<decimal> DeliveryPrice { get; set; }

通过一些反射,模型创建时会发生神奇的事情。

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);
        }
    }
}

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

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

之后,我必须调用

modelBuilder.Entity<MODEL_CLASS>().Property(c=> c.PROPERTY_NAME).HasPrecision(PRECISION,SCALE);

我通过反射调用modelBuilder.Entity()方法并将其存储在entityConfig变量中, 然后构建“c => c.PROPERTY_NAME” lambda表达式。

之后,如果十进制数可为空,我会调用

Property(Expression<Func<TStructuralType, decimal?>> propertyExpression) 

方法(我按数组中的位置调用此方法,我知道这不是理想的,非常感谢任何帮助)

如果它不可为空,我会调用

Property(Expression<Func<TStructuralType, decimal>> propertyExpression)

方法。

有了DecimalPropertyConfiguration,我调用HasPrecision方法。


3
谢谢这个。它让我免于生成数千个lambda表达式的苦恼。 - Sean
1
这个很好用,而且非常干净! 对于EF 5,我将 System.Data.Entity.ModelConfiguration.ModelBuilder 改为了 System.Data.Entity.DbModelBuilder - Colin
3
我使用 MethodInfo methodInfo = entityConfig.GetType().GetMethod("Property", new[] { lambdaExpression.GetType() }); 来获取正确的重载方法。目前看来似乎可以工作。 - fscan
3
我已将此内容整理为一个库,并使它更易于从DbContext中调用:https://github.com/richardlawley/EntityFrameworkAttributeConfig(也可通过nuget获取) - Richard
1
此处存在一个巨大的错误:MethodInfo methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[7]; 这里的 7 是硬编码的,假定该类型将是可空的十进制数。但由于某种原因,我的 .net 决定交换顺序,现在日期时间在该列表的索引 7 处,导致整个事情都失败了。 - Riz
显示剩余2条评论

53

使用 KinSlayerUY 的 DecimalPrecisionAttribute,在 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);
    }
}

在你的DbContext中:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());
}

1
如果你要对Precision进行边界检查,我建议将上限设置为28(所以在你的条件中是> 28)。根据MSDN文档,System.Decimal最多只能表示28-29位精度(https://msdn.microsoft.com/en-us/library/364x0z75.aspx)。此外,属性声明`Scale`为`byte`,这意味着你的前置条件`attribute.Scale < 0`是不必要的。 - NathanAldenSr
2
@kjbartel 是真的,一些数据库提供商支持大于28的精度;然而,根据MSDN,System.Decimal不支持。因此,将上限前提条件设置为大于28的任何值都没有意义;显然,System.Decimal无法表示那么大的数字。此外,请注意,此属性对于除SQL Server之外的数据提供程序也很有用。例如,PostgreSQL的numeric类型支持高达131072位的精度。 - NathanAldenSr
1
@NathanAldenSr 就像我说的,数据库使用定点小数(msdn),而System.Decimal是浮点数。它们完全不同。例如,拥有一个decimal(38,9)列将会很好地容纳System.Decimal.MaxValue,但是一个decimal(28,9)列则不会。没有理由将精度限制为仅为28。 - kjbartel
1
我需要为地理坐标(纬度和经度)定义精度。这是目前最干净的解决方案。现在,每个具有纬度和经度属性的类都已经用适当的属性进行了装饰,而且一切都可以正常工作,无需任何其他额外的代码。 - Michal B.
1
如果您想使用代码优先数据注释,那么这绝对是最好的解决方案。 - James
显示剩余5条评论

49

显然,您可以覆盖 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;
}

不过如果你需要对所有与价格相关的属性进行操作时,这段代码会变得相当繁琐。因此我想到了以下方法:

    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);
    }

即使基本实现没有任何作用,当你重写方法时调用基类方法是一个好的习惯。

更新:这篇文章也非常有帮助。


10
谢谢,这让我朝着正确的方向前进了。在 CTP5 中,语法已经改变,允许在同一语句中添加精度(Precision)和小数点后位数(Scale):modelBuilder.Entity<Product>().Property(product => product.Price).HasPrecision(6, 2); - Col
2
不过,如果有一种可以为所有小数设置“默认值”的方法,那不是很好吗? - Dave Van den Eynde
3
我认为调用base.OnModelCreating(modelBuilder);并非必要。根据在VS中的DbContext元数据:“此方法的默认实现不执行任何操作,但可以在派生类中重写该方法,以使模型在锁定之前进一步配置。” - Matt Jenkins
@ Dave 和 @Matt:有一个评论说调用基类是“重要的”。这是一个好习惯,但当 EF 源代码中有一个空实现时,声称它很重要是具有误导性的。这会让人们想知道基类的作用。我非常好奇什么是“重要的”,所以我反编译了 ef5.0 来查看。什么都没有。所以这只是一个好习惯。 - phil soady
@soadyp 我修改了答案以反映我在这个问题上的变化观点。希望你同意。 - Dave Van den Eynde
显示剩余4条评论

48
[Column(TypeName = "decimal(18,2)")]

按照这里的描述,这将与EF Core代码优先迁移一起工作。


3
如果您只是在模型中添加这个,您会得到“在 SqlServer 提供程序清单中找不到商店类型 'decimal(18,2)'”的错误提示信息。请注意保持原意并使语言更通俗易懂。 - Savage
@Savage 看起来这是你的数据库提供者或数据库版本的问题。 - Elnoor
@Elnoor Savage是正确的,在EF Migrations 6.x中会引发错误。传统的非Core版本不支持通过Column属性指定精度/比例,并且如果您使用DataType属性,则什么也不做(默认为18,2)。要通过Attribute在EF 6.x中使其工作,您需要实现自己的ModelBuilder扩展。 - Chris Moschini
1
@ChrisMoschini,我修改了我的答案,提到了EF Core。谢谢。 - Elnoor

33

26

从 .NET 6 开始,可以使用精度属性来替代此写法:

[Precision(precision, scale)]

在 EF Core 的旧版本中:

[Column(TypeName = "decimal(precision, scale)")]

定义:

精度 = 使用的总字符数

小数位数 = 小数点后的总数(容易混淆)

示例:

using System.ComponentModel.DataAnnotations; //.Net Core
using Microsoft.EntityFrameworkCore; //.NET 6+

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; }
    [Precision(28, 8)]
    public decimal RatingV6 { get; set; }
}

更多细节请查看:https://learn.microsoft.com/en-us/ef/core/modeling/relational/data-types


23

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

    [Precision(18, 2)]
    public decimal Price { get; set; }

请确保您需要安装EF Core 6,并执行以下using行。

Translated text:

请确保您需要安装EF Core 6,并执行以下using行。

using Microsoft.EntityFrameworkCore;

1
应该是EF Core 6+的被接受答案,因为这是独立于数据库的。 - Roy Berris

15

这行代码可能是实现同样效果的一种更简单的方法:

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

在 EF Core 7 上完美运行。 - Dany

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接