views:

66

answers:

1

Hello,

Im trying to fetch an employee row from my database. The Employee tabel have a reference to a Job tabel.

Now i want to update a Employee row with some new info. I put the new info in a new Employee object and then use this object to update the old Employee information in the database. This worked great until i added the reference between the Employee tabel and the Job tabel.

After this the Entity framework expects that my New Employee object also contains Jobs references that needs to be updated. But it does not. When i create the new Employee object i do not have the information about the Job references. So when i use the code below i get this error:

*The DELETE statement conflicted with the REFERENCE constraint "FK_EmployeeProjects_Employees". The conflict occurred in database "*", table "dbo.EmployeeProjects", column 'EmployeeId'. The statement has been terminated.*

public Employee EditEmployee(Employee employee)
    {
        try
        {
            var originalEmployee = GetEmployeeWithoutJobs(employee.Id);

            _entities.ApplyPropertyChanges(originalEmployee.EntityKey.EntitySetName, employee);

            _entities.SaveChanges();

            return originalEmployee;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Can i somehow tell the entity framework to ignore updating the Jobs references in this example?

A: 

I don't see the code you wrote inside the GetEmployeeWithoutJobs() method but i suspect you need to include the job references in your ObjectStateManager. In other words you should write something like this:

 var originalEmployee = (from e in context.EmployeeSet.Include("Jobs")
                           where e.Id = employeeId
                           select e).FirstOrDefault();

And you should use ApplyCurrentValues() instead of obsolete ApplyPropertyChanges()

Andrei
that is how the method looks allready and that does'nt work.
Poku