I am working in a project using EF 4.0.
The Employee table has a column ReferEmployeeID which contains the employee id of the employee that is referring a new employee in the system. So Employee is a self-referencing table.
Now in the case of an employee who is not added to the system is about to add and he also refers another employee in the system, the row should be added altogether.
ActualEmployee save not called yet and then ReferEmployee.Employee = ActualEmployee
I understand the issue is that both the employees actual and refer has Employee ID set to 0, but how to come around this problem.
Assuming that the
EmployeeIDin your database table is defined asINT IDENTITY, then you could do this:So basically, define your new employees with “dummy”
EmployeeIDvalues and establish the link (PeterreferencesJohnhere, by means of its “dummy” ID).When saving this into SQL Server, the Entity Framework will handle the process of getting the real
EmployeeIDvalues (which SQL Server hands out when inserting the row) and EF will maintain that link between the two employees.