![](/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.