c# - Entity Framework Code First Many to Many relationship(s) in single table -


i've been lurking around quite time, here's first question ;)

i've been playing entity framework 5.0 code first , want following:

i have 2 entities , want every entity have relation address entity in following way:

  • i have 1 address entity stores address values, doesn't have relation entities has values, instead
  • there's entity addressbook has reference address entity , coresponding entities (person, company, others in future)

here's code:

public partial class address : baseentity {     [key]     public int id { get; set; }     public string street { get; set; }     public string cityname { get; set; }     public int? postalcode { get; set; }      public virtual icollection<person> persons { get; set; }     public virtual icollection<company> companies{ get; set; } }  public partial class person : baseentity  {     [key]     public int id { get; set; }     public virtual icollection<address> addresses { get; set; }  }  public partial class company: baseentity  {     [key]     public int id { get; set; }     public virtual icollection<address> addresses { get; set; } } 

what create database schema tables:

  • address
  • addressperson (with composite primary key)
    • address_id
    • person_id
  • addresscompany
    • address_id
    • company_id
  • people
  • companies

here's want do:

  • address
  • addressbook
    • address_id (pk)
    • person_id (fk)
    • company_id (fk)
  • people
  • companies

what want have table addressbook:

public partial class addressbook {     [key]     public int id { get; set; }     public virtual address address { get; set; }     public virtual person person { get; set; }     public virtual company company { get; set; } } 

i'm not sure how define navigational properties in person , company class.

they should have icollection<address> addresses navigational property, because want them work collection of addresses without knowing underlying addressbook.

is possible dbmodelbuilder or should write code inside getter , setter of icollection<address> addresses property , addresses addressbook?

thanks!

you cannot create mapping in way entity framework understand addresses collections in person , company true navigation properties (which support eager , lazy loading , on). need indeed addressbooks collections. can add addresses not mapped , readonly helper properties then:

public partial class person : baseentity  {     [key]     public int id { get; set; }     public virtual icollection<addressbook> addressbookentries { get; set; }      public ienumerable<address> addresses     {         { return addressbookentries.select(ab => ab.address); }     } } 

(the same company.)

an alternative , in opinion better approach create common base class person , company, move addresses collection base class , have single many-to-many relationship , single join table between base class , address:

public abstract class entitywithaddresses : baseentity {     [key]     public int id { get; set; }     public virtual icollection<address> addresses { get; set; } }  public partial class person : entitywithaddresses  { }  public partial class company : entitywithaddresses  { } 

address has navigation collection new base class:

public partial class address : baseentity {     [key]     public int id { get; set; }     public string street { get; set; }     public string cityname { get; set; }     public int? postalcode { get; set; }      public virtual icollection<entitywithaddresses> entitywithaddresses { get; set; } } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -