我一直在尝试设置EF7(Beta 4)的小数精度,但一直没有成功。
我原本期望能够像下面这样做:
这似乎不可用,但我能在GitHub存储库中找到以下类:
这些只接受字符串,这个功能还没有实现或者我没有找到正确的地方吗?
编辑:刚刚意识到字符串可能是".ColumnType("decimal(10,6)")类型的解决方案,直到进一步完善,不过我仍然希望得到一些澄清,因为我不想使用字符串。
编辑:经过bricelam的澄清,我最终创建了以下扩展来暂时避免使用字符串,并且我欣赏他们方法的简洁性。
我原本期望能够像下面这样做:
modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).Precision(10, 6)
这似乎不可用,但我能在GitHub存储库中找到以下类:
没有使用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”之间哪个是正确的用法,但希望这会指引某人走向正确的方向。
modelBuilder.Properties<decimal>().Configure(x => x.HasPrecision(18, 6));
- GregoryHouseMD