Two Foreign Keys in entity framework for same table

此生再无相见时 提交于 2020-01-25 00:07:14

问题


I have two entities: Municipality and RoadSemgents. Municipality is parent and RoadSegments is a child table. I need to have two foreign Keys in RoadSegments from the same table (Municipality).

 public class Municipality
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
}


public class RoadSegments
{
    [Key]
    public int ID { get; set; }
    //ForeignKeys
    public int CodeMunicipalityLeft_ID { get; set; }
    public int CodeMunicipalityRight_ID { get; set; }

    [ForeignKey("CodeMunicipalityLeft_ID ")]
    public Municipality CodeMunicipalityLeft { get; set; }    // LOOK HERE 
    [ForeignKey("CodeMunicipalityRight_ID ")]
    public Municipality CodeMunicipalityRight { get; set; }   // AND HERE
}

How do I manage this situation? I have read some posts here for foreign keys to multiple tables but none worked for me.

来源:https://stackoverflow.com/questions/11862347/two-foreign-keys-in-entity-framework-for-same-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!