Prevent Entity Framework to Insert Values for Navigational Properties
I would like to add a 4th solution to in addition to the 3 solutions already provided in Ladislavs great answer. In facts its a detailed version of the short answer from Naor. I am working with entity framework version 6.
Assign the deparment id to the employee instead of department object
I tend to have a "foreign key value" property in addition to the navigation property in my model classes.
So on the Employee
class I have a Department
property and also an DepartmentId
of type int (make the int nullable if its possible that an Employee
has no Department
):
public class Employee
{
public int Id { get; set; }
public String EmployeeName { get; set; }
#region FK properties
public Department Department { get; set; }
public int? DepartmentId { get; set; }
#endregion
}
Would you could do now is just setting the DepartmentId
:
So instead of:
employee.Department = departmentObject;
just set:
employee.DepartmentId = departmentObject.Id;
or
employee.DepartmentId = departmentid
Now when calling SaveChanges
on the added employee, only the employee gets saved and no new department is created. But the reference from Employee
to Department
is set correctly because of the assigned department id.
More info
I usually would access the Department
object of the Employee
class only when reading / processing employees.
When creating or updating employees, I would use the DepartmentId
property of the Employee
class to assign to.
Not assigning to the Department
property of the Employee
has one downside: It could make debugging more difficult, because before calling SaveChanges
and re-reading the employees it would not be possible to see or use the Department
object of the Employee
.
Fixing entity state info in EF6
This refers to Ladislavs solution number 3.
With EF6 it is done that way:
_context.Entry(employee.Department).State = EntityState.Unchanged;
This is the way how EF works if you incorrectly use detached entities. I suppose you are using something like this:
var employee = new Employee();
employee.Department = GetDepartmentFromSomewhere(departmentId);
...
using (var context = new YourContext())
{
context.Employees.AddObject(employee);
context.SaveChanges();
}
This code prepared employee entity, added reference to existing department and saved new employee to the database. Where is the problem? The problem is that AddObject
doesn't add only employee but whole object graph. That is how EF works - you cannot have object graph where part of objects are connected to context and part of not. AddObject
adds every object in the graph as a new one (new one = insert in database). So you must either change sequence of your operations or fix state of entities manually so that your context knows that department already exists.
First solution - use the same context for loading department and saving employee:
using (var context = new YourContext())
{
var employee = new Employee();
...
context.Employees.AddObject(employee);
employee.Department = context.Departments.Single(d => d.Id == departmentId);
context.SaveChanges();
}
Second solution - connect entities to the context separately and after that make reference between entities:
var employee = new Employee();
...
var department = GetDepartmentFromSomewhere(departmentId);
using (var context = new YourContext())
{
context.Employees.AddObject(employee);
context.Departments.Attach(department);
employee.Department = department;
context.SaveChanges();
}
Third solution - correct state of the department manually so that context doesn't insert it again:
var employee = new Employee();
employee.Department = GetDepartmentFromSomewhere(departmentId);
...
using (var context = new YourContext())
{
context.Employees.AddObject(employee);
context.ObjectStateManager.ChangeObjectState(employee.Department,
EntityState.Unchanged);
context.SaveChanges();
}