Noobuntu
Noobuntu

Reputation: 177

Using Postgis' Geometry type with Entity Framework

I've been trying to build my first ever API project using ASP.NET Core.

Everything is going smoothly, except for one thing. Whenever I try to add a new scaffolded controller (after adding my models, of course), I get the following error:

The property 'Favor.Coordinates' is of type 'Geometry' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

This is self-explanatory; I've got a model class with a Geometry type (from Postgis), which seems to be causing issues for Entity Framework.

What have I done?

It's important to mention that I also added the NetTopologySuite package before generating my models, just as the tutorial mentions. Nevertheless, whenever I tried to add a scaffolded API controller using Entity Framework, I get the aforementioned error, despite the fact that NTS does support Geometry.

For further reference, here's the Favor class that the error mentions:

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

namespace FavorAPI.Models
{
    public partial class Favor
    {
        public Favor()
        {
            Payment = new HashSet<Payment>();
            Request = new HashSet<Request>();
        }

        public int Idfavor { get; set; }
        public string Description { get; set; }
        public string Title { get; set; }
        public DateTime? Creationtime { get; set; }
        public DateTime? Happeningtime { get; set; }
        public double? Price { get; set; }
        public short? Estimatedtime { get; set; }
        public short? Maxpeople { get; set; }
        public short? Currentpeople { get; set; }
        public Geometry Coordinates { get; set; } //<-- Problematic!
        public int? Idstatus { get; set; }
        public int? Idposter { get; set; }
        public int? Idcategory { get; set; }

        public virtual Category IdcategoryNavigation { get; set; }
        public virtual Individual IdposterNavigation { get; set; }
        public virtual Favorstatus IdstatusNavigation { get; set; }
        public virtual ICollection<Payment> Payment { get; set; }
        public virtual ICollection<Request> Request { get; set; }
    }
}

I have also added x => x.UseNetTopologySuite() to my DB context. Does anyone have an idea of what could I be doing wrong? I can always add the [NotMapped] attribute mentioned by the error, but I do want this attribute to be mapped.

Thanks in advance!

Edit: Creating the controllers manually (rather than through scaffolding) works and throws no errors, but eventually the same error message appears if I try to access the route to any of the controller's endpoints.

Upvotes: 3

Views: 5266

Answers (1)

Noobuntu
Noobuntu

Reputation: 177

Solved. Turns out that I wasn't supposed to include x => x.UseNetTopologySuite() solely on the DB Context file, but also on Startup.cs. Duh!

services.AddDbContext<MyDBContext>(options =>
            options.UseNpgsql(Configuration.GetConnectionString("MyDatabaseString"), x => x.UseNetTopologySuite()) );

Upvotes: 6

Related Questions