Entity Framework should not be able to insert values for navigational properties.

c# entity-framework wpf

Question

I am working on a WPF application using Entity Framework 4.0. When I tried to save the object, I got a primary key exception, but the primary key is an AutoIncremented field and I cannot understand the reason for the exception.

So after trying this and that, and a little debugging and using the SQL profiler, I found out that prior to inserting my object, a record must be inserted in the parent table, as I set the navigation property of that object.

So the crux is if an attempt to insert Employee object and set its department as Employee.Department = deptObject, then a new record is set to be inserted on department object.

Kindly suggest me someway by which navigational property objects won't be inserted in the database, any property or any method, Anything.

Thanks

1
16
8/23/2016 12:03:49 PM

Accepted Answer

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();
}
46
10/25/2011 8:32:46 AM

Popular Answer

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;


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow