Daan
Daan

Reputation: 2928

How to do migrations with EF Core 2.2 using Spatial Data?

I am trying to do migrations in Entity Framework Core 2.2 but I get some strange errors.It should work since the documentation does not say anything about mapping code or so.

This command:

dotnet ef migrations add InitialCreate

results in this error:

The property 'Point.Boundary' 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'.

I do not understand it. I have an enity, a context and all the required dependencies, including EF Core 2.2 . How do I solve it?

Project File:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0" />
    <PackageReference Include="NetTopologySuite" Version="1.15.1" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.0.2105168" />
  </ItemGroup>

Model File

using System.ComponentModel.DataAnnotations;
using NetTopologySuite.Geometries;

namespace WebApplication1.Models
{
    public class Item
    {
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public Point Location { get; set; }
    }
}

Context File

using System;
using Microsoft.EntityFrameworkCore;
using WebApplication1.Models;

namespace WebApplication1
{
    public class ItemContext : DbContext
    {
        public ItemContext(DbContextOptions<ItemContext> options) : base(options)
        {
            Console.WriteLine("Context created");
        }

        public DbSet<Item> Items { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            Console.WriteLine("OnModelCreating");
        }
    }
}

The console: enter image description here

Upvotes: 1

Views: 1214

Answers (1)

Neville Nazerane
Neville Nazerane

Reputation: 7039

Basically, the link you shared is a blog that only introduces the new features. At the end of every topic, you would find a link to the entire documentation. It seems like this suite requires an additional library based on which database you are using.

According to this documentation, you need to add the respective Spatial NuGet Package based on what your database is. (check the install section. For instance, add the Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite package if you are using SQL server. Once this is done, in your startup class, within your AddDbContext function, you can use something like this config.UseSqlServer("", x => x.UseNetTopologySuite()).

Upvotes: 3

Related Questions