我是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; }
}
答案 0 :(得分: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; }