c# - 复杂类型需要主键
问题描述
我有一个对象,其中包含具有另一个对象类型的属性,我想将其视为复杂类型。
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[ComplexType]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
在添加迁移时,我遇到了需要主键的问题(正是我想要防止的)。
实体类型Coordinate
需要定义一个主键。
编辑
出于性能原因,我希望将属性存储为Coordinate_Latitude
而Coordinate_Longitute
不是引用另一个表。
解决方案
基于这个问题(如何在 Entity Framework Core 2/C# 中实现简单的“复杂类型”?),我找到了答案:拥有的实体类型可以解决问题。
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[Owned]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
这将创建一个包含属性Id
、Coordinate_Latitued
、的表Coordinate_Longitude
。
推荐阅读
- variables - localStorage 只使用局部变量?
- python - Cloudwatch 发送到 SNS 的 JSON 有效负载中有什么?我怎样才能读取这些数据?
- python - 替换python列表中的特定模式索引项
- ssis - 无法启动程序 'DTS' OLE 已发送请求并正在等待回复?
- javascript - jQuery - 单击文件输入的父级并触发文件输入的单击事件后,它返回“递归过多”
- java - 如何在打开另一个 JFrame 时关闭一个 JFrame?
- jenkins - 我们如何通过使用与 Jenkins 中的代码相同的管道来传递(参数)三个节点从变量
- memory - 如何使用 gdb 或任何其他工具通过加载或存储操作(ARM 处理器)找到所有内存访问地址
- algorithm - BFS可以用来识别最远的顶点吗?
- scala - 由于某些字段的值未知,使用 Spark 写入 parquet 文件的数字格式异常