
Untitled
By: a guest on
May 22nd, 2012 | syntax:
None | size: 1.10 KB | hits: 14 | expires: Never
Entity Framework: Shouldn't attaching the following entity throw an exception?
Employee employee = context.Employees.First();
Address address = context.Addresses.First();
employee.Addresses.Attach(address);
Employee employee = context.Employees.First();
Address address = context.Addresses.First();
employee.Addresses.Attach(address); // System.InvalidOperationException: A
// referential integrity constraint
// violation occurred
var existingPerson = ctx.Persons.SingleOrDefault(p => p.Name = "Joe Bloggs" };
var myAddress = ctx.Addresses.First(a => a.PersonID != existingPerson.PersonID);
existingPerson.Addresses.Attach(myAddress);
// OR:
myAddress.PersonReference.Attach(existingPerson)
ctx.SaveChanges();
var existingPerson = ctx.Persons.SingleOrDefault(p => p.Name = "Joe Bloggs" };
var myAddress = ctx.Addresses.First(a => a.PersonID == existingPerson.PersonID);
existingPerson.Addresses.Attach(myAddress);
// OR:
myAddress.PersonReference.Attach(existingPerson)
ctx.SaveChanges();