如何配置OData v4模型以使多个导航道具到同一个表

时间:2016-08-03 17:15:19

标签: c# entity-framework odata-v4

我有一个拥有两个导航属性的Ownership类,每个属性引用一个ContactInfo对象,一个用于owner属性,另一个用于紧急联系属性。

[Table ("A_OWNERSHIP")]
public class Ownership {

    public Ownership() {}

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Required]
    [Column("OWNER_ID")]
    public int ownerID { get; set; }

    [Column("EMERGENCY_CONTACT_ID")]
    public int? emergencyContactID { get; set; }

    // ** other members ellided **

    public virtual ContactInfo owner { get; set; }

    public virtual ContactInfo emergencyContact { get; set; }
}

当我尝试通过我的OData服务URL获取所有权集合时:

http://localhost:52283/odata/Ownership

我收到以下错误:

ORA-00904: \"Extent1\".\"ContactInfo_ID\": invalid identifier","type":"Oracle.ManagedDataAccess.Client.OracleException"

我认为这个错误类似于这个问题中发生的错误:

Error while configuring multiple Navigation Properties to same table

问题是我使用的是OData v4,它引用了System.Web.OData.Builder而不是System.Web.Http.OData.Builder。这意味着使用Fluent API使用WithMany()调用来配置实体的答案(如链接问题中的说法)失败,因为System.Web.OData.Builder中没有WithMany()调用。

1 个答案:

答案 0 :(得分:0)

使用InverseProperty属性修饰相关对象所需的相关集合属性:

public class ContactInfo {

    public ContactInfo() {

        this.owners = new HashSet<Ownership>();
        this.emergencyContacts = new HashSet<Ownership>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    // *** other members ellided ***    

    [InverseProperty("owner")]
    public virtual ICollection<Ownership> owners { get; private set; }

    [InverseProperty("emergencyContact")]
    public virtual ICollection<Ownership> emergencyContacts { get; private set; }
}