Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core 2.2 spatial type can't be added to db migration

I'm trying to build a database with a spatial object using EF core 2.2, and i'm getting a problem with trying to create the database migrations. using https://learn.microsoft.com/en-us/ef/core/modeling/spatial , specifically:

class Country
{
    public int CountryID { get; set; }

    public string CountryName { get; set; }

    // Database includes both Polygon and MultiPolygon values
    public IGeometry Border { get; set; }
}

if i try to create a migration with this i get the following error:

The property 'Country.Border' is of an interface type ('IGeometry'). If it is a navigation property manually configure the relationship for this property by casting it to a mapped entity type, otherwise ignore the property using the NotMappedAttribute or 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

similarly if i change it to a Geometry type instead, i get:

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. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

I don't know ahead of time if my object is going to be a Point or a Line or Polygon, so it has to be generic. how do i represent that in my structure? Additionally i've seen some places say i need to add the following code:

public class MyDBContextFactory : IDesignTimeDbContextFactory<MyDBContext>
    {

        public MyDBContext CreateDbContext(string[] args)
        {
            var builder = new DbContextOptionsBuilder<MyDBContext>();
            builder.UseSqlServer(cnnString, x => x.UseNetTopologySuite());
            return new MyDBContext(builder.Options);
        }
   }

but i get the error:

'SqlServerDbContextOptionsBuilder' does not contain a definition for 'UseNetTopologySuite' and no accessible extension method 'UseNetTopologySuite' accepting a first argument of type 'SqlServerDbContextOptionsBuilder' could be found (are you missing a using directive or an assembly reference?)

even though i have the nuget package installed

like image 972
Phil Avatar asked Feb 20 '19 18:02

Phil


1 Answers

  1. Install the relevant NetTopologySuite package, it depends on the database you are using, for example you are using SqlServer so you need to install this NuGet package:

Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite 2) Configure your database to use NetTopologySuite (the code to edit is normally in StartUp.ConfigureServices()). Just add , x => x.UseNetTopologySuite() inside the options.UseSqlServer brackets

so it looks like this:

services.AddDbContext<ManagerContext>(options =>
    options.UseSqlServer(
        Configuration.GetConnectionString("DefaultConnection"),
        x => x.UseNetTopologySuite()
    )
);

I did not have to add a using to the file because I already had referenced, FYI it would be Microsoft.EntityFrameworkCore if you needed it.

If you get a reference error even after installing the NuGet package go to Manage NuGet Package and check if it's on the installed list and if it is Clean & Rebuild your solution and restart visual studio it might help.

like image 56
Tiago Silva Avatar answered Nov 06 '22 04:11

Tiago Silva