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的存储库中找到以下类:
没有使用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"之间的正确用法,但希望这会指向某个人正确的方向.
归档时间: |
|
查看次数: |
4538 次 |
最近记录: |