Reputation: 874
How to implement the field decimal(5,2)
in EntityFrameworkCore 1.0 rc2
?
HasPrecision
seems to be not available anymore?
Upvotes: 43
Views: 30339
Reputation: 2096
Since EF Core 5 you can use this:
modelBuilder.Entity<Payment>().Property(b => b.Amount).HasPrecision(5, 2);
Upvotes: 22
Reputation: 5183
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://learn.microsoft.com/en-us/ef/core/modeling/relational/data-types
Upvotes: 18
Reputation: 1537
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})");
}
}
Upvotes: 41
Reputation: 38367
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:
Upvotes: 62