Jimmacle
Jimmacle
CC#
Created by Jimmacle on 3/6/2023 in #help
❔ 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.
3 replies