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

c# entity-framework-core

Question

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

HasPrecision seems to be not available anymore?

1
27
5/23/2016 9:25:34 PM

Accepted Answer

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

42
5/23/2016 9:41:44 PM

Popular Answer

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})");
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow