实体框架7为模型构建器设置小数精度 [英] Entity Framework 7 Set decimal precision for model builder

查看:97
本文介绍了实体框架7为模型构建器设置小数精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出如何为EF7(Beta 4)设置十进制精度而没有运气的方法。

I have been trying to figure out how to set the decimal precision for EF7 (Beta 4) with no luck.

我希望这样做: / p>

I was expecting to do something like:

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).Precision(10, 6)

这似乎不可用,但是我能够在GitHub的存储库中找到以下类:

This does not appear to be available, but I was able to find the following class in the repository in GitHub:

https://github.com/aspnet/EntityFramework/blob/7.0.0-beta4/src/EntityFramework.Relational/RelationalDecimalTypeMapping.cs

没有将RelationalTypeMapping类或方法签名与其一起使用的示例。也许这只是用作映射api的一部分来检索信息?

There are no examples of using the RelationalTypeMapping classes or method signatures with them. Maybe this is just used as part of the mapping api for retrieving information?

我可能期望的另一个地方是:

Another place I might expect this to be is the following:

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().ColumnType() 

modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForSqlServer().ColumnType()

这些仅包含一个字符串,是否尚未实现此功能,或者我只是在正确的位置查看?

These only takes a string, is this functionality just not implemented yet or am I just not looking in the correct place?

编辑:刚刚意识到,字符串可能适用于.ColumnType( decimal(10,6))类型的解决方案,直到进一步构建为止,尽管我不愿为此使用字符串

Just realized that string is probably for .ColumnType("decimal(10,6)") type of solution until this is built out further, still wouldn't mind getting some clarification though as I would prefer not to use strings for this

编辑:在bricelam澄清后,我最终创建了以下扩展名以用于现在避免使用字符串,我很欣赏他们的方法的简单性:

after clarification from bricelam I ended up creating the following extension to use for now to avoid using the string, and I appreciate the simplicity of their approach:

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进行修改

我还没有测试过这些,但是我只收集了以下两个示例,它们与RC1的外观类似

I haven't tested these out yet, but I just threw together the following 2 samples of what this will probably look like with 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之间的用法,但希望这会指向正确的方向。

Since I have not yet tried this I am not sure which would be the correct usage between "HasColumnType" or "ForSqlServerHasColumnType", but hopefully this will point someone in the right direction.

推荐答案

您的解决方法是我们想要的设计。您可以设置精度,刻度,最大长度,unicode / ansi,固定/可变长度等类型,而不用一堆构面。我们决定保持简单:如果默认类型映射不是您想告诉我们使用哪种类型。曾经有传言说要重新考虑这一决定,并重新引入方面。如果您对此有强烈的感觉,我建议您创建新问题

Your workaround is the design we intended. Instead of having a bunch of "facets" you can set on a type like precision, scale, max length, unicode/ansi, fixed/variable length, etc. We decided to keep it simple: If the default type mapping isn't what you want, tell us what type to use. There have been talks of going back on this decision and reintroducing the "facets". If you feel strongly about it, I would encourage you to create a new issue.

还请注意,类型映射中目前还有许多其他错误,但是应该在发布beta5时修复。

Also note that there are a bunch of other bugs in type mapping right now, but they should be fixed by the time we release beta5.

这篇关于实体框架7为模型构建器设置小数精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆