-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enum Type Mapping with .NET 9 #3480
Comments
Just tested something else: If I query all Initiatives first from the Database (without a filter) and then do the Filtering in Memory, it works as expected:
|
This is likely some sort of issue with the JSON serialization side of your application - System.Text.Json by default serializes enums as strings (docs). EF's responsibility goes only so far as to materialize the .NET instances (Initiative instances); what happens beyond that is outside of EF's scope. I'll go ahead and close this as unrelated to EF. If you can't manage to get this to work, I recommend posting a question on stackoverflow where people will help out. If you suspect a bug around this in EF, post back here with a minimal, runnable repro and we'll revisit. |
hei @roji, thanks for your feedback on this. Something like this:
The "problem" occurs with this query here:
What I would expect here is the return of all Initiatives which have at least the status 'Published'. So:
But what I get back is something different:
As you can see, on the database level, EF most likely makes a string comparison and does not consider the enum values (integers). Here is the logged query:
|
@alainkaiser I see, I indeed completely misunderstood. To summarize, you're using a I unfortunately don't think there's really a possible solution here - this is very similar to e.g. applying |
@roji Thanks so much for your response, that's exactly what I meant. |
Hi guys
I justed created a minimal .NET 9 WebAPI project and wanted to test out the new enum type mapping with .net 9 and npgsql as described here:
https://www.npgsql.org/efcore/mapping/enum.html?tabs=with-connection-string%2Cwith-datasource
To share a little bit more about my setup:
DbContext
Model
Model Configuration with Data Seeding
Test API Endpoint
The goal of this Endpoint is to return all Initiatives which have at least the Status "Published". So far so good.
When I inspect the database, I see that a custom enum type is generated for the
InitiativeStatus
enum and that the values are stored as strings (see screenshot).But the endpoint does not what it should do. When I request data from the endpoint, the data which comes back looks like this:
So as you can see, I get back Initiatives with the Status "Submitted" and "Published". I expected to get all Initiatives on which the Status is at least "Published" (Published, Archived, Deleted).
What is missing in my code to make this work? It is obvious that the comparison is made on the string values of the enum. For this reason, the initiatives are also returned with the status Submitted.
Thanks in advance for your feedback.
The text was updated successfully, but these errors were encountered: