Entity Framework 7模型生成器设置小数精度

7
我一直在尝试设置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("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<decimal>().Configure(x => x.HasPrecision(18, 6)); - GregoryHouseMD
@reala valoro,我已经更新了细节以反映RC1的更改。 - Matt Sanders
2个回答

4

你的解决方案是我们想要的设计。与其在类型上设置一堆“面向”(如精度、比例、最大长度、Unicode/ANSI、固定/可变长度等),我们决定保持简单:如果默认的类型映射不符合您的要求,请告诉我们要使用什么类型。虽然有关于撤销这个决定并重新引入"面向"的讨论,但我们目前仍然坚持之前的方案。如果您对此持强烈意见,我鼓励您创建一个新问题

还请注意,目前存在很多类型映射中的其他错误,但它们应该会在我们发布beta5版本时得到修复。


感谢您的澄清,我已经编辑了问题,并创建了一个简单的扩展来避免字符串。我不会感到惊讶,如果这些问题在未来重新出现,但我认为这并不需要创建问题,因为您们有更重要的事情要处理 :) 我非常感谢您们所做的所有工作,并期待着更多地使用EF7! - Matt Sanders

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

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