C
C#2y ago
Jimmacle

❔ EF Core setting property with value converter to null

i have a value object
public record ShippingOrderNumber(int Number);
public record ShippingOrderNumber(int Number);
that i'm mapping to a column in EF core with
builder.Property(x => x.Number).HasConversion(number => number.Number, i => new ShippingOrderNumber(i));
builder.Property(x => x.Number).HasConversion(number => number.Number, i => new ShippingOrderNumber(i));
. my problem is that while writing to the database works fine, any operation that tries to read this column back without directly selecting it either sets it to null or just doesn't initialize it in the first place. selecting the column specifically like dbContext.ShippingOrders.Where(x => x.Number == new ShippingOrderNumber(1)).Select(x => x.Number).First() works. but something like db.ShippingOrders.AsNoTracking().OrderByDescending(x => x.Number).FirstOrDefault() or simply dbContext.ShippingOrders.First().Number returns null or blows up the change tracker because it thinks the entity's key is null. i have no idea what's going on at this point, i have two other entities with practically copy/pasted variants of this setup and they work fine. in all cases the value objects are non-null primary keys and they definitely have values in the database.
2 Replies
Jimmacle
JimmacleOP2y ago
i'm not sure where to debug it from because when it calls the constructor for the object it's always an expected value, so somewhere it's just not instantiating anything
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server