How to implement the field decimal(5,2) in EntityFrameworkCore 1.0 rc2?

烂漫一生 提交于 2019-12-18 11:46:03

问题


How to implement the field decimal(5,2) in EntityFrameworkCore 1.0 rc2 ?

HasPrecision seems to be not available anymore?


回答1:


I'm seeing some examples like this:

 entityBuilder.Property(r => r.TotalScore)
            .HasColumnType("decimal(5,2)")
            .IsRequired(true);

and the code to support this is here, so hopefully this is supported in the version you're using:

https://github.com/aspnet/EntityFramework/blob/f416dd9a71a5a6a69715b4ba40a37e6f9da751ef/src/Microsoft.EntityFrameworkCore.Relational/Metadata/Internal/RelationalPropertyBuilderAnnotations.cs




回答2:


You can add extensions for that like this:

public static class SqlServerModelBuilderExtensions
{
    public static PropertyBuilder<decimal?> HasPrecision(this PropertyBuilder<decimal?> builder, int precision, int scale)
    {
        return builder.HasColumnType($"decimal({precision},{scale})");
    }

    public static PropertyBuilder<decimal> HasPrecision(this PropertyBuilder<decimal> builder, int precision, int scale)
    {
        return builder.HasColumnType($"decimal({precision},{scale})");
    }
}



回答3:


JFYI, if somedoby is stil comming to this question (like I did)

In the current version of EF Core (2.2) there is also the Data Annotation way to do this:

public class SomeEFModelClass
{
    [Column(TypeName = "decimal(5,2)")]
    public decimal TotalScore{ get; set; }
}

Link to docs: https://docs.microsoft.com/en-us/ef/core/modeling/relational/data-types



来源:https://stackoverflow.com/questions/37400828/how-to-implement-the-field-decimal5-2-in-entityframeworkcore-1-0-rc2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!