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

1

14

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

Lamoree answered 20/2, 2019 at 18:15 Comment(0)
C
25
  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.

Castellan answered 20/2, 2019 at 18:23 Comment(5)
that's the same link i provided above, it's what made me aware of the line, but it doesn't work. I have builder.UseSqlServer(cnnString, x => x.UseNetTopologySuite()); , but i get told it can't find that reference. yes i've added the nuget packageLamoree
yeah, thanks for looking but I've done all that. your edit assumes i'm using dbContext the same as you, and i'm not. I have a class that inherits from idesigntimedbcontextfactory, like : public class MyDBContextFactory : IDesignTimeDbContextFactory<MyDBContext>. In there i use the CreateDBContext method. it should work the same, but it doesn't look the same. see my edited questionLamoree
Ugh, my bad, you're right. I was focusing on Install-Package NetTopologySuite instead of the EFCore one you mentioned above.Lamoree
I'd just like to add for others that you also need to install the correct version of the package based on the version of EF Core you're using.Infuscate
As an additional note: if you are using the design factory pattern you need to add 'UseNetTolopogySuite()' there as well.Hearten

© 2022 - 2024 — McMap. All rights reserved.