efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

bug(?): custom implementation of `NpgsqlArrayConverter` claims to be unable to be instantiated when creating migrations

Open QuantumToasted opened this issue 2 years ago • 0 comments

Hi there. I'm attempting to migrate some code that formerly used the old INpgsqlArrayConverter interface to instead use the new NpgsqlArrayConverter<> class, and I seem to be hitting a snag.

public sealed class HashSetValueComparer<T>() : ValueComparer<HashSet<T>>((l, r) => l!.SequenceEqual(r!), x => x.GetHashCode(), x => x.ToHashSet());
public sealed class Uuid7Converter() : ValueConverter<Uuid7, Guid>(static x => x.ToGuid(), static x => new Uuid7(x));

public sealed class Uuid7HashSetConverter : NpgsqlArrayConverter<HashSet<Uuid7>, Uuid7[], Guid[]>
{
    public Uuid7HashSetConverter()
        : base(new Uuid7Converter())
    { }
}

// meanwhile, in the DbContext...
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
    configurationBuilder.Properties<Uuid7>().HaveConversion(typeof(Uuid7Converter));
    configurationBuilder.Properties<HashSet<Uuid7>>().HaveConversion(typeof(Uuid7HashSetConverter), typeof(HashSetValueComparer<Uuid7>));
}

Upon generating a migration, I'm hit with this cryptic error:

Unable to create a 'DbContext' of type ''. The exception 'Cannot create an instance of value converter type 'Uuid7HashSetConverter'. Ensure that the type can be instantiated and has a parameterless constructor, or use the overload of 'HasConversion' that accep
ts a delegate.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

I'm unsure why it is complaining about being unable to instantiate my converter - it clearly has a parameterless constructor. Is this an issue with efcore.pg, or should I bark further up the tree to efcore itself? I don't even know where to begin trying to source this issue. Perhaps I messed up something while trying to migrate to the new class from the interface and it's just plain old PEBKAC?

Thanks in advance.

QuantumToasted avatar Nov 27 '23 11:11 QuantumToasted