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

Mat*_*ers 7 entity-framework-core

我一直试图弄清楚如何设置EF7(Beta 4)的小数精度没有运气.

我期待做类似的事情:

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).Precision(10, 6)
Run Code Online (Sandbox Code Playgroud)

这似乎不可用,但我能够在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() 
Run Code Online (Sandbox Code Playgroud)

要么

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForSqlServer().ColumnType()
Run Code Online (Sandbox Code Playgroud)

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

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

编辑:在从bricelam澄清后,我最终创建了以下扩展以供使用,以避免使用字符串,我感谢他们的方法简单:

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

用法示例:

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().DecimalPrecision(10,6);
Run Code Online (Sandbox Code Playgroud)

编辑:对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})");
    }
Run Code Online (Sandbox Code Playgroud)

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

bri*_*lam 4

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

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