efcore.pg: Nested (through navigation property) Value Conversion (enum to string) causes exception.

From @JakenVeina on July 28, 2018 5:52

When selecting an enum value from a related record, conversions defined for that property are not evaluated, and cause an InvalidCastException (wrapped by an InvalidOperationException).

  1. Problem Query: https://hastebin.com/ugufozituy.cs
  2. ModerationActionSummary.FromEntityProjection: https://hastebin.com/ukudanudoc.cs
  3. ModerationActionEntity: https://hastebin.com/lemedabofe.cs
  4. InfractionEntity: https://hastebin.com/wihotayoyu.cs
  5. ModixContext.OnModelCreating(): https://hastebin.com/iqayacecox.cs
  6. InfractionSummary: https://hastebin.com/padakicese.cs
  7. Full Codebase: https://github.com/JakenVeina/MODiX/tree/efcore-issue-12834

Specifically, the Type = entity.Infraction.Type, line in snippet 2 above is causing the following exception: https://hastebin.com/equfozunaj.

When the .Type property is being selected from the root element in an expression, as in snippet 6 above, the value conversion works fine.

Furthermore, I can work around the error by changing Type = entity.Infraction.Type in snippet 2, to the following lovely expression. Type = Enum.Parse<InfractionType>(entity.Infraction.Type.ToString())

This seems to work great. The whole query still evaluates to one SQL query. But surely, there ought to be a way to support the more sensible version of the expression.

Using:

  1. netcoreapp2.1
  2. Microsoft.EntityFrameworkCore (2.1.1)
  3. Mpgsql.EntityFrameworkCore.PostgreSQL (2.1.1)
  4. Windows 8.1 Pro
  5. Visual Studio 2017 15.7.5.

Copied from original issue: aspnet/EntityFrameworkCore#12834

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Can close this as duplicate of https://github.com/aspnet/EntityFrameworkCore/issues/13025 I am adding code to unwrap nullableExpression

Since I did not have PostgreSQL installed, I tried it with SqlServer and it worked fine, moving it here. If this repros in other providers then feel free to open an issue in EF Core repo.