【问题标题】:Entity Framework 7 Set decimal precision for model builderEntity Framework 7 为模型构建器设置小数精度
【发布时间】:2015-05-22 05:03:27
【问题描述】:

我一直试图弄清楚如何为 EF7(Beta 4)设置小数精度,但没有成功。

我期待做类似的事情:

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).Precision(10, 6)

这似乎不可用,但我能够在 GitHub 的存储库中找到以下类:

https://github.com/aspnet/EntityFramework/blob/7.0.0-beta4/src/EntityFramework.Relational/RelationalDecimalTypeMapping.cs

没有使用 RelationalTypeMapping 类或方法签名的示例。也许这只是用作检索信息的映射 api 的一部分?

我可能期望的另一个地方如下:

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().ColumnType() 

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForSqlServer().ColumnType()

这些只需要一个字符串,是这个功能还没有实现还是我没有找到正确的地方?

编辑:刚刚意识到 string 可能是 .ColumnType("decimal(10,6)") 类型的解决方案,直到进一步构建,但仍然不介意得到一些澄清因为我不想为此使用字符串

编辑:在得到 bricelam 的澄清后,我最终创建了以下扩展程序以供现在使用以避免使用字符串,我很欣赏他们的方法的简单性:

public static RelationalPropertyBuilder DecimalPrecision(this RelationalPropertyBuilder propertyBuilder, int precision, int scale)
    {
        return propertyBuilder.ColumnType($"decimal({precision},{scale})");
    }

使用示例:

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().DecimalPrecision(10,6);

编辑:对 RC1 进行修改

我还没有对这些进行测试,但我只是将以下 2 个样本放在一起,看看 RC1 可能会是什么样子

    public static PropertyBuilder DecimalPrecision(this PropertyBuilder propertyBuilder, string precision, string scale)
    {
        return propertyBuilder.HasColumnType($"decimal({precision},{scale})");
    }

    public static PropertyBuilder SqlDecimalPrecision(this PropertyBuilder propertyBuilder, string precision, string scale)
    {
        return propertyBuilder.ForSqlServerHasColumnType($"decimal({precision},{scale})");
    }

由于我还没有尝试过,我不确定“HasColumnType”或“ForSqlServerHasColumnType”之间的正确用法,但希望这会为某人指明正确的方向。

【问题讨论】:

  • 由于我所有的小数属性都具有相同的精度,有什么方法可以在一行中对所有这些属性进行实现吗?我在 EF6 中使用它:modelBuilder.Properties&lt;decimal&gt;().Configure(x =&gt; x.HasPrecision(18, 6));
  • @reala valoro,我已经更新了细节以反映 RC1 的变化

标签: entity-framework-core


【解决方案1】:

您的解决方法是我们想要的设计。您可以设置精度、比例、最大长度、unicode/ansi、固定/可变长度等类型,而不是一堆“方面”。我们决定保持简单:如果默认类型映射不是什么你想要,告诉我们使用什么类型。已经讨论过要撤回这一决定并重新引入“方面”。如果你对此有强烈的感觉,我会鼓励你create a new issue

另外请注意,目前类型映射中还有许多其他错误,但它们应该在我们发布 beta5 时修复。

【讨论】:

  • 感谢您的澄清,我使用创建的简单扩展名编辑了问题以避免字符串。看到这些在路上重新引入,我一点也不感到惊讶,但我认为这不值得制造一个问题,因为你们有更大的鱼要炸:) 我感谢你们所做的所有工作,并且我真的很期待更多地使用 EF7!
【解决方案2】:

根据 EF RC1,显示的示例似乎已过时。

这是我在小数字段上设置精度的方法。

假设我有一个实体

public class Review
{
    public int ReviewId { get; set; }
    public decimal TotalScore { get; set; } //I want a precision field in DB
    public DateTime CreatedOn { get; set; }
    [Timestamp]
    public byte[] RowVersion { get; set; }
}

然后在我的上下文类中,在创建模型时,我会实例化映射(我可以在那里进行映射,但我喜欢将其分开)

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options ) : base(options)
    {
    }

    public DbSet<Review> Reviews { get; set; }
    //etc.

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //Mappings
        new ReviewMap(modelBuilder.Entity<Review>());
        //etc..
    }
}

然后是映射。请记住使用模型扩展所在的命名空间:

using Microsoft.Data.Entity; //here is where the extensions are
public class ReviewMap
{
    public ReviewMap(EntityTypeBuilder<Review> entityBuilder)
    {
        entityBuilder.HasKey(r => r.ReviewId);

        //Using the column type extension
        entityBuilder.Property(r => r.TotalScore)
            .HasColumnType($"decimal(5,2)")
            .IsRequired(true);

        //and this has nothing to do with the example but it's interesting
        //to show how to use Sql command to automatically fulfil a value 
        //when adding a new Entity
        entityBuilder.Property(r => r.CreatedOn)
            .ValueGeneratedOnAdd()
            .HasDefaultValueSql("GETUTCDATE()")
            .IsRequired(true);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2016-08-21
    • 2017-10-25
    • 2015-11-07
    • 1970-01-01
    • 2019-05-12
    • 2011-04-30
    相关资源
    最近更新 更多