【发布时间】:2021-11-24 14:55:41
【问题描述】:
我正在使用 Openlayers 并尝试将几何信息保存在我的数据库中。
当我在 openlayers 地图上绘制多边形时,生成的对象(特征)就是这种格式
{
"type": "Polygon",
"coordinates": [
[
[
54.86572265625,
74.0013854318592
],
[
53.59130859375,
73.62159408606237
],
[
53.96484375,
73.16953636227885
],
[
55.986328125,
73.59679245247814
]
]
]
}
上面的对象有 2 个属性。 {type: string, coordinates: someNestedArray}
我将此对象传递给我的 API 以将其保存在数据库中。但是在定义坐标属性类型时遇到了问题。
基本上它是 float[][][] 类型,所以我创建了我的 EF 模型类,如下所示
public class Geometry
{
public string Type { get; set; }
public float[][][] Coordinates { get; set; }
}
当我尝试获取/更新时,EF 抛出错误
{"The property 'Geometry.Coordinates' could not be mapped, because it is of type 'float[][][]'
which is not a supported primitive type or a valid entity type. Either explicitly map this property,
or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."}
错误很明显。 EF 无法自行映射这些不受支持的类型。如何明确定义映射?或者有什么办法可以让它工作吗? float[][][] 类型正确吗?
提前致谢。
【问题讨论】:
-
数组的深度取决于Type的值。对于 MultiPolygon,它是
[][][][],而对于 Point,它是[] -
@Mike 感谢您的回复。我的要求仅适用于多边形。
标签: entity-framework entity-framework-core openlayers