npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when including list of Point

RousseauRemi opened this issue · comments

Dear Team,

I am currently experiencing an exception with a List of Point while utilizing Entity Framework (EF) and PostgreSQL with PostGIS. The issue arose during the transition from .NET 7 to .NET 8, as the functionality was operating correctly in the former. Upon moving to .NET 8, I am unable to retrieve data due to the exception detailed in the related issue: #2975.

To facilitate the reproduction of the problem, I have prepared a minimal repository: https://github.com/workpioupiou/ErrorNpgsql. By executing the command "dotnet ef migrations add Initial --project ErrorNpgsql", the following error is generated:

"Unable to create a 'DbContext' of type ''. The exception 'When building an array mapping over 'Point', the JsonValueReaderWriter for element mapping 'NpgsqlGeometryTypeMapping`1' is incorrect ('NpgsqlJsonGeometryWktReaderWriter' instead of 'NetTopologySuite.Geometries.Point').' was thrown while attempting to create an instance."

As a temporary workaround, I have moved the List of Point into a new class, PositionPoint, which contains an ID and a property point. However, this solution required the deletion of the migrations files and the database.

I would greatly appreciate any insights or potential solutions to resolve this issue without having to delete the migrations files and the database.

public class Zone
{
    public Guid Id { get; set; }
    public List<Point> Positions { get; set; }
}

Become :

public class Zone
{
    public Guid Id { get; set; }
    public List<PositionPoint> Positions { get; set; }
}

public class PositionPoint
{
    public Guid Id { get; set; }
    public Point Position { get; set; }
}

Thank you for your time and assistance.

I'm also having a similar error related to byte arrays. In my case it's when querying with an optional ULID column with that is mapped to nullable bytea, using Contains. It has the same stack trace with the referenced issue.

System.InvalidOperationException: When building an array mapping over 'Nullable'1', the JsonValueReaderWriter for element mapping 'NpgsqlByteArrayTypeMapping' is incorrect ('JsonConvertedValueReaderWriter'2' instead of 'NUlid.Ulid').
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping.NpgsqlArrayTypeMapping'3.CreateParameters(String storeType, RelationalTypeMapping elementMapping)

It also worked without problems with Npgsql 7/EFCore 7.

Is the repro provided by the OP enough or should I also create one?

// Edit: If anyone else comes across this post before it's fixed (since Google shows this at the top), you can create your own contains expression to get around this, this should help: https://stackoverflow.com/a/9678266/483084