如何处理实体中没有其他关系的嵌套对象



考虑以下类:

public class Country{
public string Name {get;set;}
public Coordinate Coordinate {get;set;}
}
public class Coordinate{
public Latitude {get;set;}
public Longitude {get;set;}
}

现在,当我创建一个迁移时,它创建了两个表:CountryCoordinate,并在这两个表之间有一个映射。

Table: Country
[id, name, coordinateId]
Table: Coordinate
[id, latitude, longitude]

这感觉很可疑,因为坐标与其他任何东西都没有关系。也可以存储在同一个表中。

我觉得更好的方法是有一个表[Country]与所有的字段:

Table: Country
[id, name, coordinate_latitude, coordinate_longitude]

在EF中是否可以接受有很多嵌套对象的表,这些表仅由其主父对象使用的数据填充?或者是否有一种更有效的"平坦化"对象的方法?

https://learn.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/implement-value-objects

有一个如何在主表中将值对象设置为列的示例:

orderConfiguration。OwnsOne (p =比;p.Address).Property (p =祝辞p.Street) .HasColumnName("ShippingStreet";

orderConfiguration。OwnsOne (p =比;p.Address).Property (p =祝辞p.City) .HasColumnName("ShippingCity";

为什么不使用SqlGeography

public class Country{
public string Name {get;set;}
public SqlGeography Coords {get;set;}
}

否则无论如何都可以将类扁平化。

public class Country{
public string Name {get;set;}
public double Latitude {get;set;}
public double Longitude {get;set;}
}

最新更新