0

I'm not sure the best way to create this kind of relation ship. I have these two entities for this example.

Person & Address

public class Person
{
   public string Name { get; set; }
   public virtual ICollection<PersonAddressLink> HomeAddresses { get; set; }
   public virtual ICollection<PersonAddressLink> WorkAddresses { get; set; }
}

public class Address
{
   public string AddressString {get; set; }
   public virtual ICollection<Person> People { get; set; }
}

and a link table, needed because it contains other info.

public class PersonAddressLink
{
  public Address HomeAddress { get; set; }
  public Address WorkAddress { get; set; }

  public int SomeOtherInt { get; set; }
  public string SomeOtherString { get; set; }

}

The problem is EF doesn't know how to separate the entities on person.HomeAddresses / person.WorkAddresses. I have tried mergin HomeAddress & WorkAddresses into a single collection like this:

   public virtual ICollection<PersonAddressLink> WorkAddresses { get; set; }

but it still won't work.

I'm just looking for advice on how to lay something like this out to get it working with EF Code first.

I hope that makes sense.

Thanks

Steven Yates
  • 2,400
  • 3
  • 30
  • 58
  • Do you have mapping configurations, or you letting EF work it automatically? Also, I cannot really tell if Address is a table or a complex type.. since I do not see a PK. Possibly related to http://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-table – deherch Jul 28 '14 at 15:57
  • I'm just letting EF do the work. It is related to the other question... I will have to try it out tomorrow as I'm not near my laptop. I'll post more tomorrow but still accepting answers, thanks @deherch – Steven Yates Jul 28 '14 at 20:01

1 Answers1

0

Late reply but I got the mapping correct by creating the table in SQL Management studio and using the reverse engineer functionality which generated the Code First. I need to use two separate entities.

Steven Yates
  • 2,400
  • 3
  • 30
  • 58