Reputation: 71
I'm new to EF and trying to delete an entry from a parent Person table and 2 child tables; Name and Address...(cascade). When I Attach to context I get the; An entity object cannot be referenced by multiple instances of IEntityChangeTracker . When I erase the Attach method I get the; The object cannot be deleted because it was not found in the ObjectStateManager error. Thanks for any idea because I'm out.
using (var context = new PersonEntities())
{
ObjectContext oc = ((IObjectContextAdapter)context).ObjectContext;
Email[] DeleteName = SelectedEntries[CurName].Names.ToArray();
Phone[] DeletePhone = SelectedEntries[CurAddress].Addresses.ToArray();
foreach (name name in DeleteName)
{
foreach(Address address in DeleteAddress)
{
foreach (Person person in SelectedEntries)
{
context.Names.Attach(name);
oc.DeleteObject(name);
context.Addresses.Attach(address);
oc.DeleteObject(address);
context.Persons.Attach(person);
oc.DeleteObject(person);
oc.ObjectStateManager.ChangeObjectState(name,
System.Data.EntityState.Deleted);
oc.ObjectStateManager.ChangeObjectState(address,
System.Data.EntityState.Deleted);
oc.ObjectStateManager.ChangeObjectState(person,
System.Data.EntityState.Deleted);
}
}
}
Upvotes: 1
Views: 115
Reputation: 3456
You have two copies of the database context. The second is the one in your code sample. The first is the one we can't see but know exists because of the errors you get. It is the context used to generate the SelectedEntities. Every time you call new PersonEntities you get a separate context which tracks the state of all its objects. The first error you get says databaseContext1 owns those objects. You can't give them to databaseContext2. This is prevented for consistency. The second error occurs because databaseContext2 doesn't know about the objects you are trying to delete. You need to use the same PersonEntities to delete objects that you used to fetch them from the database.
If this isn't clear, post the code you use to fetch the database objects originally.
Upvotes: 0
Reputation: 16199
To me it looks like DeleteAddress is created in a different context.
You should get DeleteAddress in the same using block. Otherwise you have it in another context, then you are also trying to attach it to this context to delete it, which explains the errors.
Do your get and delete from the same context.
Upvotes: 1
Reputation: 76
your entities are in the objectcontext before of delete?, if not you need attach your entities, example:
for the framework 4.0:
EntityKey ekProducto = new EntityKey("DbCursoEntity.Productos", "Id", producto.Id);
using (dbCurso = new DbCursoEntity())
{
producto.EntityKey = ekProducto; //Añado la key a la entidad.
dbCurso.Productos.Attach(producto); //Enlanzo la entidad al ObjectSet mediante Attach.
dbCurso.Productos.DeleteObject(producto); //Elimino la entidad.
if (dbCurso.SaveChanges() > 0)
return true;
else
return false;
}
older framework:
EntityKey ekProducto = new EntityKey("DbCursoEntity.Productos", "Id", producto.Id);
using (dbCurso = new DbCursoEntity())
{
producto.EntityKey = ekProducto; //Añado la key a la entidad.
dbCurso.Attach(producto); //Enlanzo la entidad mediante Attach.
dbCurso.DeleteObject(producto); //Elimino la entidad.
if (dbCurso.SaveChanges() > 0)
return true;
else
return false;
}
in this examples, my entity not are in the context, for add the context I need added an entitykey with the id.
Upvotes: 0