© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
2 replies
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()
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()
db.ShippingOrders.AsNoTracking().OrderByDescending(x => x.Number).FirstOrDefault()
or simply
dbContext.ShippingOrders.First().Number
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ EF Core: Owned property with converter expression cannot be converted
C#CC# / help
3y ago
Generic converter bounding in EF Core
C#CC# / help
17mo ago
Ef core value comparer
C#CC# / help
4y ago