![](/img/trans.png)
[英]EF Core 3.1 The property 'Geometry.UserData' could not be mapped
[英]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不支持。
该应用程序使用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");
}
}
有没有办法把map类型的?
谢谢!
通过配置中的附加转换进行处理。
就我而言,源是一个视图,因此无论如何都不使用 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)
);
}
}
这是我用于 EF 6、.Net Core 6 的内容:
services.AddDbContext<AWSMariaSQLDbContext>(options => options.UseMySql(configuration.GetConnectionString("AWSMariaSQLDataConnection"), Microsoft.EntityFrameworkCore.ServerVersion.Parse("10.6.10-mariadb"),
options => options.UseNetTopologySuite())
);
然后在我的 model 生成器中:
entity.Property(e => e.Location)
.HasColumnName("Location")
.HasColumnType("point");
我的Model:
public Point Location { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.