考慮以下類:
public class Country{
public string Name {get;set;}
public Coordinate Coordinate {get;set;}
}
public class Coordinate{
public Latitude {get;set;}
public Longitude {get;set;}
}
現在,當我創建遷移時,它會創建兩個表:Country并Coordinate在兩個表之間建立映射。
Table: Country
[id, name, coordinateId]
Table: Coordinate
[id, latitude, longitude]
這感覺很陰暗,因為坐標與其他任何東西都沒有關系。它也可以存盤在同一個表中。
我覺得更好的方法是擁有 1 個[Country]包含所有欄位的表:
Table: Country
[id, name, coordinate_latitude, coordinate_longitude]
在 EF 中是否可以接受很多嵌套物件的表,這些表填充了僅由其主要父級使用的資料?或者有沒有辦法“展平”更有效的物件?
uj5u.com熱心網友回復:
https://docs.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");
uj5u.com熱心網友回復:
為什么不使用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;}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/322822.html
下一篇:簡單的選擇C#命令-初學者
