Skip to content

[8.0.0]: Defining a collection value conversion in ConfigureConventions throws InvalidOperationException #32682

@QuantumToasted

Description

@QuantumToasted

Disclaimer: I am currently working on trying to get a minimal repro project actually up and running, but as I have narrowed down the source of the issue, I believe the code defined below may suffice as a minimal reproduction of the error. I will likely update this issue message or add a new commit when it's done.

I am consuming the following third-party type Snowflake which I am storing in my database via a converter, which works fine for single columns. I'm trying to store a HashSet<Snowflake> as a long[] (as converting to long makes it bigint instead of numeric(0,20)). I've defined the conversion as follows with a ValueConverter:

public sealed class SnowflakeHashSetConverter() : ValueConverter<HashSet<Snowflake>, long[]>(
    x => x.Select(y => (long) y.RawValue).ToArray(),
    x => x.Select(y => new Snowflake((ulong) y)).ToHashSet());

and in my ConfigureConventions method:

configurationBuilder.Properties<HashSet<Snowflake>>()
    .HaveConversion(typeof(SnowflakeHashSetConverter)/*, typeof(HashSetValueComparer<Snowflake>)*/);

*note: HashSetValueComparer is a relic of an older attempt at valueconversion that I kept just in case and is commented out - it does not affect the outcome or exception generated

Upon attempting to add a new migration (with no previous migrations, mind you) generates the following exception:

Unable to create a 'DbContext' of type ''. The exception 'The model must be finalized and its runtime dependencies must be initialized before 'GetTypeMapping' can be used. Ensure that either 'OnModelCreating' has completed or, if using a stand-alone 'ModelBu
ilder', that 'IModelRuntimeInitializer.Initialize(model.FinalizeModel())' was called.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Converting all these columns to, say ulong[] or long[] and removing the usage of the ValueConverter entirely results in a successful migration generation, but then I lose the convenience aspects of HashSet, namely preventing duplicate values from being added in a single line.

I am unsure if this behavior is intentional, or maybe specific to efcore.pg (the provider I'm using). If it is intentional somehow, could the exception or other type of error message be more clear about the source of the error or the model, property etc which is the cause? Otherwise one will have to check every detail of every model to find the source of the issue. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions