【问题标题】:How to do migrations with EF Core 2.2 using Spatial Data?如何使用空间数据使用 EF Core 2.2 进行迁移?
【发布时间】:2019-05-07 06:29:23
【问题描述】:

我正在尝试在 Entity Framework Core 2.2 中进行迁移,但出现了一些奇怪的错误。should work 因为文档没有说明任何关于映射代码的内容。

这个命令:

dotnet ef 迁移添加 InitialCreate

导致此错误:

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

我不明白。我有一个实体、一个上下文和所有必需的依赖项,包括 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");
        }
    }
}

控制台:

【问题讨论】:

  • 您使用的是哪个数据库?你能发布你的启动类的数据库配置部分吗?

标签: entity-framework .net-core ef-core-2.2


【解决方案1】:

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

根据本文档,您需要根据您的数据库添加相应的Spatial NuGet 包。 (检查 install 部分。例如,如果您使用 SQL Server,请添加 Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite 包。完成此操作后,在您的启动类中,在您的 AddDbContext 函数中,您可以使用一些东西像这样config.UseSqlServer("", x =&gt; x.UseNetTopologySuite())

【讨论】:

    猜你喜欢
    • 2021-12-29
    • 2019-07-14
    • 2020-10-02
    • 1970-01-01
    • 2021-09-29
    • 2019-04-11
    • 2017-08-18
    • 1970-01-01
    • 2020-07-09
    相关资源
    最近更新 更多