I have a value object named "WithdrawFee"
public Fee WithdrawFee { get; private set; }
and the Fee value object is :
public class Fee : ValueObject<Fee>
{
public decimal Value { get; private set; }
public static Fee FromDecimal(decimal value) => new Fee(value);
public Fee(decimal value)
{
if (value < 0)
throw new ArgumentException("Fee is not valid");
Value = value;
}
public override int ObjectGetHashCode() => Value.GetHashCode();
public override bool ObjectIsEqual(Fee otherObject) => Value == otherObject.Value;
public override string ToString() => $"{Value}";
public static implicit operator decimal(Fee fee) => fee.Value;
}
that configured with fluent bellow :
builder.Property(c => c.WithdrawFee)
.HasConversion(c => c.Value, v => Fee.FromDecimal(v)).HasColumnType("decimal(18,8)");
THE PROBLEM:
the problem is when I run query below :
decimal testValue = 10.541m;
var result = await db.Networks.AsNoTracking()
.Where(c => c.WithdrawFee > testValue )
.ToListAsync();
it ran this sql query on db (I got it from sql profiler):
SELECT *
FROM [Networks]
WHERE CAST([WithdrawFee] AS decimal(18,2)) > CAST(10.541 AS decimal(18,2))
as you can see, it cast values to decimal(18,2) !
Why ef cast decimals ?
How can I fix this ?
ef core version : 5.0.10
.net version : 3.1