0

Here's my table structure:

Person

PersonID int

Address

AddressId int
Address varchar

PersonAddress

PersonID int primary key references Person.PersonID
AddressID int primary key references Address.AddressID

As you can see there is no relation between Person and Address. When I add these 3 tables to EF 6.0, it does not add the PersonAddress table but adds a relationship between Person and Address. I am deeply confused.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3726933
  • 329
  • 2
  • 17
  • That's exactly how it's supposed to work in EF, and that's the relationship you're trying to have anyway in objects – AD.Net Jul 29 '14 at 16:20
  • How are you "adding" this to Entity Framework? Are you using code-first? If so, please **show us** your classes! Typically, EF will model the *conceptual* model of a `Person` having multiple addresses, and an `Address` potentially belonging to multiple people - but it'll "hide" the details of how that's handled in the database. No need to be worried! – marc_s Jul 29 '14 at 16:20
  • @marc_s Thanks. I am using database first model. So EF is intelligently assuming that addresses can be shared across persons and one person can have multiple addresses. I thought it will add all the tables I tell it to add and show it in the EDMX diagram. This is totally cool. Any documentation on it? Thanks. I will play with what it generated and see if that satsifies my needs. – user3726933 Jul 29 '14 at 16:28
  • If you have a database model, you should be able to right-click on your design surface and go `Generate Database from Model`. You'll get the SQL that would be used to generate your database - I'm **sure** you'll see that "link" table in there, too! – marc_s Jul 29 '14 at 16:30
  • Docs: use MSDN and the usual Microsoft Dev Cenver for EF - that's the best docs you can find – marc_s Jul 29 '14 at 16:31
  • possible duplicate of [Can Entity Framework handle many to many relationship without an intersection object?](http://stackoverflow.com/questions/13588574/can-entity-framework-handle-many-to-many-relationship-without-an-intersection-ob) – Gert Arnold Jul 29 '14 at 16:36

1 Answers1

0

EF is doing exactly what you want. In EF you have a relationship between OBJECTS, not SCHEMA. The object relationship between Person and Address can be accomplished without the need of a third object. This reduces the complexity and makes the code easier to manage. EF will handle translating the relationship back and forth to the schema as needed.

Josh
  • 1,724
  • 13
  • 15