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

Error when including list of Point

Open RousseauRemi opened this issue 1 year ago • 2 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: https://github.com/npgsql/efcore.pg/issues/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.

RousseauRemi avatar May 15 '24 07:05 RousseauRemi

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

hakanderyal avatar Jun 24 '24 10:06 hakanderyal

Hi, any news on this ?

I have the exact same problem and it prevents me to migrate my project correctly in .NET 8 .

roddone avatar Jul 22 '24 14:07 roddone

Thanks for raising this and sorry it took so long to get looked at. I can confirm there's a bug here - I'm pushing a fix and will backport it to the next release of 8.0.x.

roji avatar Oct 29 '24 16:10 roji

@roji, made my day!

martin-hirsch avatar Oct 29 '24 16:10 martin-hirsch

@hakanderyal I'm not sure your problem is the same as this one... Can you please open a new issue with a repro?

roji avatar Oct 29 '24 16:10 roji

I believe it's the same issue, it occurs at the same location which you've changed. I'll open a new one if it continues after the release.

Thanks!

hakanderyal avatar Oct 29 '24 16:10 hakanderyal