0

我首先是 EF 代码的新手,并试图强制 ICollection 导航属性使用我手动创建的特定关系表来映射实体。

我被迫这样做的原因是我的所有表上都有一个 TenantId 列。这有可能吗?

[Table("Tenants")]
public class Tenant
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    // This should use ContactAddressesForTenants table
    public virtual ICollection<Address> ContactAddresses {get; set;}

}

[Table("BusinessProfiles")]
public class BusinessProfile
{
    [Key, ForeignKey("Tenant")]
    public int TenantId { get; set; }

    public virtual Tenant Tenant { get; set; }

    // This should use the AddressesForBusinessProfiles table
    public virtual ICollection<Address> ProfileAddresses { get; set; }
}

[Table("Addresses")]
public class Address
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ForeignKey("Tenant")]
    public int TenantId { get; set; }
}

[Table("ContactAddressesForTenants")]
public class ContactAddressForTenant
{
    [ForeignKey("Tenant")]
    public int TenantId { get; set; }

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

    public virtual Tenant Tenant { get; set; }

    public virtual Address Address {get; set;}
}

[Table("AddressesForBusinessProfiles")]
public class AddressForBusinessProfile
{
    [ForeignKey("BusinessProfile")]
    public int TenantId { get; set; }

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

    public virtual BusinessProfile BusinessProfile { get; set; }

    public virtual Address Address { get; set; }
}
4

1 回答 1

0

如果我理解正确的话。您可以尝试在模型中更改它。

在哪儿:

// This should use ContactAddressesForTenants table
public virtual ICollection<Address> ContactAddresses {get; set;}
// This should use the AddressesForBusinessProfiles table
public virtual ICollection<Address> ProfileAddresses { get; set; }

改成:

public virtual ICollection<ContactAddressForTenant> ContactAddresses {get; set;}
public virtual ICollection<AddressForBusinessProfile> ProfileAddresses { get; set; }
于 2013-02-19T23:30:40.697 回答