复杂类型需要主键 [英] Complex Type requires primary key

查看:90
本文介绍了复杂类型需要主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,该对象包含具有另一个对象类型的属性,我想将其视为复杂类型。

I have an object that contains an attribute with the type of another object, which I want to treat as Complex Type.

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; }
}

在添加迁移时,我遇到了一个主键是

While adding a migration, I ran into the problem that a primary key is required (exactly what I want to prevent).

实体类型坐标需要定义主键。

The entity type Coordinate requires a primary key to be defined.

编辑

出于性能原因,我希望将属性存储为 Coordinate_Latitude Coordinate_Longitute 而不是引用另一个表。

For performance reasons I want the properties being stored as Coordinate_Latitude and Coordinate_Longitute instead of having a reference to another table.

推荐答案

基于此问题(如何在Entity Framework Core 2 / C#中实现简单的复杂类型?),我找到了答案:拥有实体类型可以解决问题。 / p>

Based on this question (How do I implement a simple "complex type" in Entity Framework Core 2/C#?), I found the answer: Owned entity types do the trick.

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

This creates a table containt the attributes Id, Coordinate_Latitued, Coordinate_Longitude.

这篇关于复杂类型需要主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆