For instance I have persons, locations and a personlocation table to link the two. I also have a role and personrole table.
Tables:
Person (personid, name)
Personlocation (personid, locationid)
Location (locationid, description)
Personrole (personid, roleid)
Role (roleid, description)
EF will give me persons, roles and location entities.
Since EF will NOT generate the personlocation and personrole entity types, they cannot be
used in the query.
QUESTION: how can i add a Person object and return personid and then add that id with 3 roleids into Personrole table/association?
e.g.
Person p = new Person();
p.name = "John"
......
entity.AddToPersons(p);
for(var roleid in Roleid)
entity.AddtoRoles(roleid)?
EF doesn’t need to map the join tables when there is nothing but the FK’s – that is a good thing. It is smart enough to do a “silent-join” behind the scenes.
You just need to use the “Roles” association on the “Person” entity. You don’t need to add straight to the “Role” table.
E.g:
Entity Framework is pretty smart. It will see there is a new role, add that first, then add a person, then add the record in the join table based on the id for the role that was just created.
HTH.
EDIT – In response to comment:
Very similar to above.
Just fetch the existing role from the DB, and add it to the person: