如果您有一個 EF/asp.net Core 應用程式,其中包含帶有價格服務的廣告。每個廣告可以有許多服務(在一組預定義的選擇中,如理發、指甲油等),并且每個廣告的價格都是可變的。你如何形成多對多關系?
public class Ad {
...
...
// the list of serviceTypes to choose from to add to your Ad.
public List<ServiceType> Services { get; set; )
}
public class ServiceType {
...
public string ServiceName { get; set; }
// I can't set price here since every ad has its own price (varying) for the given serviceType!
public List<Ad> Ad { set; get; }
}
uj5u.com熱心網友回復:
這不再是 EF 可以為您隱式處理的兩個物體之間的多對多關系,而是三個物體之間的兩個一對多關系。
AdServiceType創建一個具有兩個 FK ( Ad, ServiceType) 和價格欄位的中介(或任何其他適當的名稱)。然后AdServiceType充當您的廣告和服務型別之間的連接關系。
uj5u.com熱心網友回復:
基于@Flater答案,您應該創建一個中間類:
public class Ad
{
public long Id { get; set; }
// the list of serviceTypes to choose from to add to your Ad.
public ICollection<AdServiceType> AdServiceTypes { get; set; } = new HashSet<AdServiceType>();
}
public class ServiceType
{
public long Id { get; set; }
public string ServiceName { get; set; }
// I can't set price here since every ad has its own price (varying) for the given serviceType!
public ICollection<AdServiceType> AdServiceTypes { set; get; } = new HashSet<AdServiceType>();
}
public class AdServiceType
{
public long AdId { set; get; }
public long ServiceTypeId { set; get; }
public Ad Ad { set; get; }
public ServiceType Service { set; get; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/474355.html
