实体框架7为模型构建器设置小数精度

时间:2015-05-22 05:03:28

标签: entity-framework-core

我一直试图找出如何设置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()

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

编辑:刚刚意识到字符串可能是.ColumnType(“十进制(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”之间的正确用法是什么,但希望这会指向某人正确的方向。

2 个答案:

答案 0 :(得分:4)

您的解决方法是我们想要的设计。而不是拥有一堆&#34; facet&#34;您可以设置精度,比例,最大长度,unicode / ansi,固定/可变长度等类型。我们决定保持简单:如果默认类型映射不是您想要的,请告诉我们什么类型使用。一直在讨论回到这个决定并重新引入&#34; facets&#34;。如果您对此感到强烈,我会鼓励您create a new issue

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

答案 1 :(得分:1)

根据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);
    }
}
相关问题