如何将 EF Core 的 MySQL 几何类型 属性 映射到 .NET Core 几何类型 属性?

How to map MySQL Geometry type property with EF Core to .NET Core Geometry type property?

我有一个应用程序 (.NET Core 3.1),它从数据库中提取数据并使用它。

以前数据库是 Ms Sql 服务器,我能够使用 NetTopologySuite 来处理数据库中的空间数据。

现在数据库已更改为 MySQL,NetTopologySuite doesn't support that

应用程序使用 MySql.Data.EntityFrameworkCore 连接器。

定义:

public class ExampleObjectDefinition{

        public Geometry GeoLocation { get; set; }
}

配置:

public class ExampleObjectDefinitionConfiguration: IEntityTypeConfiguration<ExampleObjectDefinition>
{
        public void Configure(EntityTypeBuilder<ExampleObjectDefinition> builder)
        {
            builder.ToTable("Example_Table");


            builder.Property(_ => _.SpatialTypedLocation)
                   .HasColumnName("SPATIAL_LOCATION");
        }
}

有没有办法映射类型?

谢谢!

在配置中使用额外的转换进行处理。

在我的例子中,源是一个视图,所以 setter 无论如何都没有被使用。

public class ExampleObjectDefinitionConfiguration: IEntityTypeConfiguration<ExampleObjectDefinition>
{
        public void Configure(EntityTypeBuilder<ExampleObjectDefinition> builder)
        {
            builder.ToTable("Example_Table");

            builder.Property(_ => _.SpatialTypedLocation)
                   .HasColumnName("SPATIAL_LOCATION")
                   .HasConversion<MySqlGeometry?>
                    (
                       v => new MySqlGeometry(v.PointOnSurface.X, v.PointOnSurface.Y), 

                       value => value == null ? null : new Point(value.Value.XCoordinate.Value, value.Value.YCoordinate.Value)
                    );

        }
}