我有一个表,其中有 2 个外键指向同一个父表 Edge.StartStationId 和 Edge.EndStationId。
我正在尝试将这些映射到实体框架中的对象,但找不到似乎可以解决问题的解决方法。我在父级(站)上找到了一些使用 2 个集合的解决方案,我对此不感兴趣。
站(父)类:
public partial class Station
{
public Station()
{
this.Reservations = new List<Reservation>();
this.StationMaintenances = new List<StationMaintenance>();
}
public int ID { get; set; }
public int TypeId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public decimal StationLat { get; set; }
public decimal StationLong { get; set; }
public bool IsOperational { get; set; }
public bool IsActive { get; set; }
public DateTime CreatedDate { get; set; }
public virtual BatteryStorage BatteryStorages { get; set; }
public virtual List<Reservation> Reservations { get; set; }
public virtual List<StationMaintenance> StationMaintenances { get; set; }
public virtual List<Edge> Edges { get; set; }
public virtual StationType StationType { get; set; }
}
边缘(子)类:
public partial class Edge
{
public int ID { get; set; }
public int StartStationId { get; set; }
public virtual Station StartStation { get; set; }
public int EndStationId { get; set; }
public virtual Station EndStation { get; set; }
public decimal Distance { get; set; }
public decimal Time { get; set; }
public bool IsActive { get; set; }
}
边图类,在 OnModelCreating 中添加。
public EdgeMap()
{
// Primary Key
this.HasKey(t => t.ID);
// Properties
// Table & Column Mappings
this.ToTable("Edges");
this.Property(t => t.ID).HasColumnName("ID");
this.Property(t => t.StartStationId).HasColumnName("StartStationId");
this.Property(t => t.EndStationId).HasColumnName("EndStationId");
this.Property(t => t.Distance).HasColumnName("Distance");
this.Property(t => t.Time).HasColumnName("Time");
this.Property(t => t.IsActive).HasColumnName("IsActive");
// Relationships
//this.HasOptional(t => t.StartStation)
// .WithMany(t => t.Edges)
// .HasForeignKey(d => d.StarStationId);
//this.HasOptional(t => t.EndStation)
// .WithMany(t => t.Edges)
// .HasForeignKey(d => d.EndStationId);
}
例外:
在模型生成期间检测到一个或多个验证错误:System.Data.Entity.Edm.EdmAssociationType: : 多重性与关系“Edge_EndStation”中角色“Edge_EndStation_Target”中的引用约束冲突。因为从属角色中的所有属性都不可为空,所以主体角色的多重性必须为“1”。