简体   繁体   English

实体框架多对象关系

[英]Entity framework multiple object relation

I have a table with 2 foreignkeys to the same parent table, Edge.StartStationId and Edge.EndStationId. 我有一个表,该表具有指向同一父表Edge.StartStationId和Edge.EndStationId的2个外键。

I am trying to map theese to objets in entity framework, but can't find a workaround which seems to fix the problem. 我正在尝试将theese映射到实体框架中的objets,但是找不到似乎可以解决该问题的解决方法。 I've found some solutions using 2 collections on the parent (Station), which I'm not interested in. 我在父级(工作站)上使用了2个集合,找到了一些解决方案,对此我不感兴趣。

Station (parent) class: 站(父母)班:

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; }
}

Edge (child) class: 边缘(子)类:

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; }
}

The edge map class, which is added in OnModelCreating. 边缘贴图类,已在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);
}

Exception: 例外:

One or more validation errors were detected during model generation: System.Data.Entity.Edm.EdmAssociationType: : Multiplicity conflicts with the referential constraint in Role 'Edge_EndStation_Target' in relationship 'Edge_EndStation'. 在模型生成期间检测到一个或多个验证错误:System.Data.Entity.Edm.EdmAssociationType::多重性与关系'Edge_EndStation'中的Role'Edge_EndStation_Target'中的引用约束发生冲突。 Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. 因为从属角色中的所有属性都是不可为空的,所以主体角色的多重性必须为'1'。

I think the particular error you posted is complaining about the HasOptional . 我认为您发布的特定错误是在抱怨HasOptional Since the foreign keys ( StartStationId and EndStationId ) are non-nullable, it's expecting a required mapping. 由于外键( StartStationIdEndStationId )不可为空,因此期望使用必需的映射。 Try changing it to HasRequired , or change the type of StartStationId and EndStationId to int? 尝试将其更改为HasRequired ,还是将StartStationIdEndStationId的类型更改为int? so that it knows the navigation properties can be null. 以便知道导航属性可以为null。

As for the Edges collection, what should it contain? 至于Edges集合,它应该包含什么? Any Edge that references the Station with either the StartStationId or EndStationId values? 是否使用StartStationIdEndStationId值引用Station任何Edge If so, I don't think you can do that with a single collection. 如果是这样,我认为您无法通过一个集合来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM