Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading INpgsqlArrayConverter to PrimitiveCollection #3152

Open
payoff opened this issue Apr 10, 2024 · 0 comments
Open

Upgrading INpgsqlArrayConverter to PrimitiveCollection #3152

payoff opened this issue Apr 10, 2024 · 0 comments

Comments

@payoff
Copy link

payoff commented Apr 10, 2024

I am in the process of upgrading a huge working project from Entity Framework Core 7 to version 8, which utilizes a significant number of INpgsqlArrayConverter instances to store various types of data in PostgreSQL arrays. We also make use of Jsonb, but we do not like the idea to migrate all of our existing data.

One of the converters we have been using is shown below:

public class DictionaryListStringsConverter : ValueConverter<Dictionary<string, List<string>?>?, List<string>?>, INpgsqlArrayConverter
{
    private static readonly KeyValueListStringsConverter _converter = new();

    public DictionaryListStringsConverter()
        : base(v => ToProvider(v), v => FromProvider(v)) {}

    public ValueConverter ElementConverter => _converter;

    private static List<string>? ToProvider(Dictionary<string, List<string>?>? dictionary)
    {
        return dictionary?.Select(x => _converter.ConvertToProvider(x)).Cast<string>().ToList();
    }

    private static Dictionary<string, List<string>?>? FromProvider(List<string>? list)
    {
        var dict = list?.Select(x => _converter.ConvertFromProvider(x)).Cast<KeyValuePair<string, List<string>?>>().ToList();
        return dict == null ? null : new Dictionary<string, List<string>?>(dict);
    }
}

Now the result code is something like:
repro code gist

However, when attempting to create a migration, we encounter the following error:

Unable to create a 'DbContext' of type 'TestContext'. The exception 'The 'Dictionary<string, List>' property 'TestEntity.AllowedTransitions' could not be mapped because the database provider does not support this type.

Personally, I find the old implementation far superior as it aligns seamlessly with the PostgreSQL way of working.
Any assistance or guidance would be greatly appreciated.

Regards,
Michele.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant