skip to Main Content

I have wrote my own source generator which generates some boilerplate converters, including a value converter for EF Core.

Here’s a short example:

[GenerateConverters]
public partial record Height(int Value) : StrongTypeComparableRecord<int, Height>(Value)
{
    public partial class HeightValueConverter;
    public partial class HeightNewtonsoftJsonConverter;
    public partial class HeightTypeConverter;
}

And the generated file looks like this:

public partial record Height
{
    // Automatically generated static Create method
    public static Height Create(int value) => new(value);
    
    partial class HeightValueConverter : ValueConverter<Height, int>
    {
        public HeightValueConverter() 
            : base(e => e.Value, e => new Height(e)) { }
    }

    partial class HeightNewtonsoftJsonConverter : IntJsonConverter<Height>
    {
        protected override Height CreateInstance(int value) => new(value);
        protected override int GetValue(Height instance) => instance.Value;
    }

    partial class HeightTypeConverter : TypeSafeConverter<Height, int>
    {
        protected override Height ConvertFromType(int value) => Height.Create(value);
        protected override int ConvertToType(Height value) => value.Value;
    }
}

These two classes are located inside a class library of my business project, and there is a project which is responsible for the database stuff (EF Core). This project references the class library, but not the generator project.

The EF Core project defines some IEntityTypeConfigurations with something like this:

profileInformationBuilder.Property(e => e.Height)
  .HasConversion<Height.HeightValueConverter>();

Now when I try to create a migration, I get the following error:

Unable to create a ‘DbContext’ of type ‘…Data.EFCore.Contexts.EfCoreContext’. The exception ‘The ‘Height’ property ‘ProfileInformation.Height’ could not be mapped because the database provider does not support this type. Consider converting the property value to a type supported by the database using a value converter. See https://aka.ms/efcore-docs-value-converters fo
r more information. Alternately, exclude the property from the model using the ‘[NotMapped]’ attribute or by using ‘EntityTypeBuilder.Ignore’ in ‘OnModelCreating’.’ was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

After a little chatting with GPTs, they all say that its because the design time approach of EF Core is conflicting here, but I didn’t find a real solution, nor do I see how others solved these issues.

Does anyone know how to solve this? Tell me if more code is needed

2

Answers


  1. Chosen as BEST ANSWER

    I accidentally fixed it. I was searching a little on Github in the efcore repository and found a post of someone who has problems with the latest version of "Microsoft.CodeAnalysis.Workspaces" and related packages. Completly different problem, but I gave it a try and it seems to work. The error is gone after going back to version 4.9.2.


  2. Don’t know if this qualifies as a "great" answer by stack-overflow standards, as I’m a bit unsure myself, but it seems that the <TProvider> generic that .HasConversion(...) accepts should equal the datatype that will be written to the "store" (Database), which in your case would be an int.

    Have you tried .HasConversion<int>(..);, passing an instance of one of your "Height <-> int" converters as the first argument?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search