I have a class supplied to me via Nuget. I don't have the source.
public class SpecialProductResult
{
public int id { get; set; }
public decimal SpecialPercent {get;set;}
}
I want to populate a list of SpecialProductResult from a stored procedure
So in my DbContext I have
public DbQuery<SpecialProductDto> SpecialProducts { get; set; }
I populate the list using
var specialProducts = connect.SpecialProducts.FromSql("spGetSpecialProducts").ToList()
In the error log I see messages like
No type was specified for the decimal column ‘“SpecialPercent”’ on entity type ‘“SpecialProductResult”’. This will cause values to be silently truncated if they do not fit in the default precision and scale. Explicitly specify the SQL server column type that can accommodate all the values using ‘ForHasColumnType()’.
I looked at this question and wanted to try
modelBuilder.Entity<SpecialProductResult>().Property(o => o.GoldPercent).HasPrecision(18,4)
But there is no property .HasPrecision
What should I try?
[Update]
I tried Ivan Stoev's answer but received a runtime error
The entity type 'SpecialProductResult' cannot be added to the model because a query type with the same name already exists
Hi, You can use following sql statement to change "MyColumn" decimal precision. And when you change decimal (5, 2) to decimal (6, 2), the precision and decimal places are not reduced. So the data will not change.
Precision is the number of digits in a number. Scale is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a precision of 5 and a scale of 2. In SQL Server, the default maximum precision of numeric and decimal data types is 38.
Currently EF Core does not provide database independent way of specifying the numeric type precision and scale (similar to EF6 HasPrecision
).
The only way to do that is to use HasColumnType and specify the database specific type. If you need to support different databases, you have to use if
statements and different HasColumnType
for each database type.
For SqlServer, it would be
modelBuilder.Query<SpecialProductResult>()
.Property(o => o.GoldPercent)
.HasColumnType("decimal(18,4)");
public class Part
{
public Guid PartId { get; set; }
public string Number { get; set; }
[Column(TypeName = "decimal(18,4)")] // <--
public decimal Size { get; set; }
}
source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With