Vogen icon indicating copy to clipboard operation
Vogen copied to clipboard

Generated EF Core converters are incompatible with AOT compiled models

Open Vespion opened this issue 8 months ago • 0 comments

Describe the bug

When using the EF Core converters, compiling the model for AOT causes multiple build errors.

When the backing type is a string (although I've not tested with other types except uint), EF Core uses the == and != operators directly, which fail because it tries to compare a primitive string from the database with the value object. While not ideal, it can be fixed by setting the toPrimitiveCasting to CastOperator.Implicit.

However, the rest of the errors are accessibility errors. When EF Core compiles a model and encounters a property that has a value converter, EF Core directly calls the converter methods (in this case __Deserialize), which fail as they are private and cannot be accessed by the generated model code.

(An example of this error Database\CompiledModel\StarSystemEntityType.cs(80,64): error CS0122: 'StarSystemId.__Deserialize(uint)' is inaccessible due to its protection level)

The most straightforward fix I can see would be changing the compiled method to be internal or public rather than private (although perhaps best hidden behind a configuration option to prevent needlessly exposing the method unless you need it)

It should also be noted that this only occurs when compiling an AOT model; compiling the model without the AOT flag will not cause any errors as instead of calling the methods directly, EF core just constructs the converter object normally.

Steps to reproduce

  1. Create a ValueObject
  2. Use it as a property in an EF Core model (ensuring that EF Core is configured to use the generated value converter)
  3. Attempt to compile the model using dotnet ef dbcontext optimize --nativeaot
  4. The EF tool will generate invalid code in the output folder

Reproduction.zip

Expected behaviour

The generated compiled model can call Vogen's converters successfully in AOT mode.

Vespion avatar Apr 21 '25 13:04 Vespion