0

so I am implementing EF in my project. In my unit testing I have come accross an issue I cannot seem to figure out on my own...

I have...

class Employee{
   Guid uid
name: "Foo";
Department: dempartmentObj;
}

class department{
    Guid uid
name: "food prep";
desc: "preps food";
}

now as part of the logic from the program I am creating a new employee and linking them into the new department

so I

var x = new employee();
 x.department = context.get(uid); //ef context call to retrieve a department

then use upsert logic to save int he event the code is used to modify instead of add

context.Entry(x).state = x.uid == guid.empty ?
      EntityState.Added: EntityState.Modified;

if(x.uid == Guid.Empty)
          x.uid = Guid.newGuid();

context.SaveChanges();

with unit testing I have confirmed that it works when Creating both new employee and department

however when retrieving the innner object I run into primary key violations trying to save the new employee...

asuppa
  • 571
  • 1
  • 11
  • 27
  • Have you tried calling `SaveChanges()` twice?, create your department > SaveChanges > create your Employee > SaveChanges. If that works it means something is wrong with the relationship and employee can't get the FK from department. – kondas Mar 24 '15 at 14:13
  • @kondas I am able to save each object independantly and update(when retrieved) however when creating the outside object new and retrieving the inside object from the database I receive the error. – asuppa Mar 24 '15 at 18:36
  • You need to update your question and provide a more specific code, your actual classes and if you use code first your context code so we can assist you better. What's the specific exception?. Is your "outside" definition attached to the context or have you set it's entry to unchanged? – kondas Mar 24 '15 at 18:47

0 Answers0