gpt4 book ai didi

c# - 复杂类型需要主键

转载 作者:行者123 更新时间:2023-12-03 22:12:21 25 4
gpt4 key购买 nike

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

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_LatitudeCoordinate_Longitute而不是引用另一个表。

最佳答案

基于这个问题( How do I implement a simple "complex type" in 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 .

关于c# - 复杂类型需要主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53416869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com