Raul
Raul

Reputation: 177

Mapping geometries with EF Core 2.2, Npgsql and NetTopologySuite

I`m trying to map this class:

using NetTopologySuite.Geometries;
using System.Collections.Generic;

namespace Project.API.Models
{
    public class Geo
    {
        public int Id { get; set; }
        public IEnumerable<Geometry> Geometries{ get; set; }
    }
}

But when I try to add a new migration I'm having this error:

The property 'Geometry.UserData' could not be mapped, because it is of type 'object' which is not a supported primitive type or a valid entity type...

Doing some research (https://www.npgsql.org/efcore/mapping/nts.html) I found that I need to use:

services.AddDbContext<DataContext>(x => x.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"), o => o.UseNetTopologySuite()));

instead of

services.AddDbContext<DataContext>(x => x.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

But adding the o => o.UseNetTopologySuite() I have the error:

'NpgsqlDbContextOptionsBuilder' does not contain a definition for 'UseNetTopologySuite'

I think this i related to version issues but the versions that I'm using are exactly as suggested in this issue on github (https://github.com/npgsql/efcore.pg/issues/1024).

NuGet:

enter image description here

Upvotes: 1

Views: 2884

Answers (1)

Shay Rojansky
Shay Rojansky

Reputation: 16722

As the documentation says, you need to reference the Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite package. Also note that if you use EF Core 2.2, you will have to use NetTopologySuite 1.15.x and not 2.0.0; the latter will only work with EF Core 3.0/3.1.

Upvotes: 3

Related Questions