I've been going around the block a couple of times on this, so I'm taking a fresh approach. I would like to figure out if it possible to have a single entity that is on the many-side of multiple 0-to-many relationships. This is what I'm trying to do:
A Client has 0-to-many Phones
public class Client
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ClientId { get; set; }
public string Name { get; set; }
public virtual ICollection<Phone> Phones { get; set; }
}
A Business has 0-to-many Phones
public class Business
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int BusinessId { get; set; }
public string Name { get; set; }
public virtual ICollection<Phone> Phones { get; set; }
}
And here is Phones:
public class Phone
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PhoneId { get; set; }
public string Number { get; set; }
}
Of course, the problem with the Phones property in Business/Client is that this creates FK's in Phone to both Client and Business, which clutters-up Phones.
So, I saw another poster try creating a manual join table, but it seemed to be geared to the many-side participating in one relationship:
public class ClientPhone
{
public int ClientID { get; set; }
public int PhoneID { get; set; }
public virtual Client Client { get; set; } // One Client
public virtual Phone Phone { get; set; } // One Phone
}
Should I split Phone up into ClientPhones and BusinessPhones 'normal' entities using traditional 0-to-many relationships. If someone could give me some advice on the cleanest way to to model this, it would be very appreciated.
Thanks!
I think you can model this using table per hierarchy method ,
This will create one table for both client and business phone with a discriminator column. Then you can easily separate client and business phones.