[英]Optional One to many relationship
我正在嘗試使用流利的API進行可選的一對多關系。 但是由於出現此錯誤,它似乎不起作用:
InBuildingNavigator.Data.Models.ConnectionPointRoute_Segment::多重性與關系“ ConnectionPointRoute_Segment”中的角色“ ConnectionPointRoute_Segment_Target”中的引用約束沖突。 因為從屬角色中的所有屬性都是不可為空的,所以主體角色的多重性必須為'1'。
這是模型創建:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ConnectionPointRoute>()
.HasKey(c => new {c.ConnectionPointId, c.RouteId, c.SegmentId});
modelBuilder.Entity<ConnectionPoint>()
.HasMany(c => c.ConnectionPointRoutes)
.WithRequired(x => x.ConnectionPoint)
.HasForeignKey(c => c.ConnectionPointId);
modelBuilder.Entity<Route>()
.HasMany(c => c.ConnectionPointRoutes)
.WithRequired(x => x.Route)
.HasForeignKey(c => c.RouteId);
modelBuilder.Entity<Segment>()
.HasMany(c => c.ConnectionPointRoutes)
.WithOptional(s => s.Segment)
.HasForeignKey(s => s.SegmentId);
}
這是模型:
public class ConnectionPointRoute
{
public int ConnectionPointId { get; set; }
public int RouteId { get; set; }
public int? SegmentId { get; set; }
public int Position { get; set; }
public ConnectionPoint ConnectionPoint { get; set; }
public Route Route { get; set; }
public Segment Segment { get; set; }
}
public class Segment
{
public Segment()
{
ConnectionPointRoutes = new List<ConnectionPointRoute>();
}
public int SegmentId { get; set; }
public int ConnectionPointIdEnd { get; set; }
public string ConnectionName { get; set; }
public string ConnectionInformation { get; set; }
public string Image { get; set; }
public string Direction { get; set; }
public ICollection<ConnectionPointRoute> ConnectionPointRoutes { get; set; }
}
有什么想法嗎?
這是因為您試圖在ConnectionPointRoute
上定義一個包含可以為空的SegmentId
的組合主鍵。 您不能在可為空的列上定義主鍵。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.