Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Entity Framework to Insert Values for Navigational Properties

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

like image 839
manav inder Avatar asked Oct 25 '11 05:10

manav inder


People also ask

How do I remove a navigation property in Entity Framework?

. Clear() method is very good. It deletes all navigation properties in database. That's simple rather than foreach loop.

How do I use navigation properties in Entity Framework?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data. A navigation property definition includes the following: A name.

Why navigation properties are virtual?

If you define your navigation property virtual , Entity Framework will at runtime create a new class (dynamic proxy) derived from your class and uses it instead of your original class. This new dynamically created class contains logic to load the navigation property when accessed for the first time.

What are scalar and navigation properties in Entity Framework?

Basically a scalar property is mapped to a column (int, string, ...) A navigation property is mapped to a relation. e.g Order. OrderDetails brings you to all ORderDetails of a specific order.


2 Answers

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();
}
like image 198
Ladislav Mrnka Avatar answered Sep 22 '22 03:09

Ladislav Mrnka


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;
like image 42
Martin Avatar answered Sep 24 '22 03:09

Martin