如何使用空间数据对 EF Core 2.2 进行迁移?

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

我正在尝试在 Entity Framework Core 2.2 中进行迁移,但我得到了一些奇怪的东西 errors.It should work 因为文档没有说明任何关于映射代码的信息。

这个命令:

dotnet ef 迁移添加 InitialCreate

导致此错误:

属性'Point.Boundary'是接口类型('IGeometry')。如果它是导航 属性 通过将其转换为映射实体类型手动配置此 属性 的关系,否则使用 [=] 中的 NotMappedAttribute 或 'EntityTypeBuilder.Ignore' 忽略 属性 44=].

我不明白。我有一个实体、一个上下文和所有必需的依赖项,包括 EF Core 2.2 。我该如何解决?

项目文件:

<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>

模型文件

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; }
    }
}

上下文文件

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");
        }
    }
}

控制台:

基本上,您分享的link是一个只介绍新功能的博客。在每个主题的末尾,您会发现 link to the entire documentation。根据您使用的数据库,该套件似乎需要一个额外的库。

根据此文档,您需要根据您的数据库添加相应的 Spatial NuGet 包。 (检查 install 部分。例如,如果您使用 SQL 服务器,请添加 Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite 包。一旦完成,在您的启动 class,在你的 AddDbContext 函数中,你可以使用这样的东西 config.UseSqlServer("", x => x.UseNetTopologySuite())