views:

31

answers:

2

Hello All:

I have 3 tables a. Employee (EmpID (pk), EmpName) b. Department (DepID (pk), DepName) c. EmployeeDepartmentMapping (ID (pk), EmpID(fk), DepID(fk))

When I am inserting new employee, I want to insert correspoding Emp-Dep mappings in the EmployeeDepartmentMapping table using entity framework 3.5. Can any body help/tell me how to insert many-many relationships using entity framework in database?

Thanks, Ashwani

+1  A: 
Employee emp = new Employee();

EmployeeDepartmentMapping edm = new EmployeeDepartmentMapping();
edm.Emp = emp;

if u know the dept id. then

edm.Dept = _ent.Department.where(i => i.deptId == dept_id).first();

_ent.AddToEmplyee(emp);
_ent.AddToEmployeeDepartmentMapping(edm);
franklins
Thanks Franklins, it worked. I want one more favor. Can u tell me how to update foreign key reference. Suppose I have a table which stores employee and its role mapping where roles is coming from different table. Now I want to change the role of the employee. The new table has employeeId and RoleId as foreign keys. Any help?
Ashwani K
A: 
emp.<RolesEmpMapTable>Refernce.Load();
emp.<RolesEmpMapTable>.Role = _ent.Roles.where(i=>i.roleId == role_id).first();
franklins