C
C#10mo ago
Samuel Duarte

EF Core HasPrecision()

Has anyone ever defined precision for decimal numbers in EF Core without using attributes? Even defining the value for some reason is truncated at "decimal(18,2)" when I do the migrations.
10 Replies
Samuel Duarte
Samuel DuarteOP10mo ago
builder
.Property(transfer => transfer.Amount)
.HasColumnType("decimal")
.HasPrecision(14, 2);

# Migrations code generated

Amount = table.Column<decimal>(type: "decimal(18,2)", nullable: false)
builder
.Property(transfer => transfer.Amount)
.HasColumnType("decimal")
.HasPrecision(14, 2);

# Migrations code generated

Amount = table.Column<decimal>(type: "decimal(18,2)", nullable: false)
leowest
leowest10mo ago
No description
leowest
leowest10mo ago
worked for me using postgresql
leowest
leowest10mo ago
No description
Jimmacle
Jimmacle10mo ago
you can also specify the column type directly as decimal(x,y)
leowest
leowest10mo ago
I didn't even need to specify the columntype on my first try efcore automagically did its work I wonder if his issue is a version/db issue
Samuel Duarte
Samuel DuarteOP10mo ago
I used SqlServer and still the precision is not defined
leowest
leowest10mo ago
and what is the version of the efcore nuget you're using?
Samuel Duarte
Samuel DuarteOP10mo ago
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
leowest
leowest10mo ago
yeah I cannot reproduce it sorry maybe some one else knows about it

Did you find this page helpful?