实体框架代码第一个外键不同的列密钥

时间:2015-07-22 17:04:56

标签: asp.net entity-framework foreign-keys

因此,地址的PK为 Id EntityKey 的外键引用供应商表格Id,因为供应商的物理建筑物都有地址。

当我查询供应商时,我无法返回相关的地址。

注意:我不想更改架构。

[Table("EE.Address")]
public partial class Address {
    [Key]
    public Guid Id { get; set; }

    [StringLength(50)]
    public string Address1 { get; set; }

    [StringLength(50)]
    public string Address2 { get; set; }

    [StringLength(50)]
    public string City { get; set; }

    [StringLength(5)]
    public string State { get; set; }

    [StringLength(30)]
    public string PostalCode { get; set; }

    [StringLength(10)]
    public string CountryCode { get; set; }

    public Guid EntityKey { get; set; }

    [ForeignKey("EntityKey")] 
    public virtual Vendor Vendor { get; set; }
    // EntityKey should be Vendor.Id
}


[Table("EE.Vendor")]
public partial class Vendor {
    [Key]
    public Guid Id { get; set; }

    [StringLength(50)]
    public string Name { get; set; }

    [StringLength(50)]
    public string AlternativeName { get; set; }

    [StringLength(50)]
    public string VendorCode { get; set; }

    [StringLength(50)]
    public string TaxId { get; set; }

    public Guid VendorTypeKey { get; set; }

    public Guid? ParentKey { get; set; }

    [ForeignKey("Id")]
    public virtual Address Address { get; set; }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

[Table("EE.Address")]
public partial class Address {
    [Key]
    public Guid Id { get; set; }

    [StringLength(50)]
    public string Address1 { get; set; }

    [StringLength(50)]
    public string Address2 { get; set; }

    [StringLength(50)]
    public string City { get; set; }

    [StringLength(5)]
    public string State { get; set; }

    [StringLength(30)]
    public string PostalCode { get; set; }

    [StringLength(10)]
    public string CountryCode { get; set; }

    [ForeignKey("Vendor")]
    public Guid EntityKey { get; set; }

    public virtual Vendor Vendor { get; set; }
    // EntityKey should be Vendor.Id
}


[Table("EE.Vendor")]
public partial class Vendor {
    [Key]
    public Guid Id { get; set; }

    [StringLength(50)]
    public string Name { get; set; }

    [StringLength(50)]
    public string AlternativeName { get; set; }

    [StringLength(50)]
    public string VendorCode { get; set; }

    [StringLength(50)]
    public string TaxId { get; set; }

    public Guid VendorTypeKey { get; set; }

    public Guid? ParentKey { get; set; }

    [ForeignKey("Address")]
    public Guid AddressId {get; set; }

    public virtual Address Address { get; set; }
}