'How to implement the field decimal(5,2) in EntityFrameworkCore 1.0 rc2?
How to implement the field decimal(5,2) in EntityFrameworkCore 1.0 rc2 ?
HasPrecision seems to be not available anymore?
Solution 1:[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:
Solution 2:[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})");
}
}
Solution 3:[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
Solution 4:[4]
Since EF Core 5 you can use this:
modelBuilder.Entity<Payment>().Property(b => b.Amount).HasPrecision(5, 2);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AaronLS |
| Solution 2 | Andrés Robinet |
| Solution 3 | Tolbxela |
| Solution 4 |
