I'm trying to delete an object using EntityFramework 5 but i get this error.
The object cannot be deleted because it was not found in the ObjectStateManager
I am using the Remove()
method as DeleteObject()
is not present in EF5.
Can anyone help what am I missing?
This does not work for Remove
localDb.Customers.Remove(new Customer() { CustomerId = id });
localDb.SaveChanges();
Another thing I tried from msdn to change the state to Deleted. But here it gives an error saying all the fields should be present. Is it necessary to get the complete record then delete?
var customer = new Customer(){ CustomerId = id };
localDb.Customers.Attach(customer);
localDb.Entry(customer).State = EntityState.Deleted;
localDb.SaveChanges();
Any inputs?
You can fetch the row from the database and then delete it, but this causes 2 round trips to the database.
If you want to do it in one hit, your second version with Attach
will work - as long as the entity is not already loaded into the context.
The error you are getting is caused by EF validation which runs before any database write.
You can turn it off temporarily like this:
bool oldValidateOnSaveEnabled = localDb.Configuration.ValidateOnSaveEnabled;
try
{
localDb.Configuration.ValidateOnSaveEnabled = false;
var customer = new Customer { CustomerId = id };
localDb.Customers.Attach(customer);
localDb.Entry(customer).State = EntityState.Deleted;
localDb.SaveChanges();
}
finally
{
localDb.Configuration.ValidateOnSaveEnabled = oldValidateOnSaveEnabled;
}
if you retrieved object from database then it's already attached to the context so you can delete by:
db.myTable.Remove(model);
but
if you just received model from edit or delete view by post or generated it yourself to avoid 2 trips to database then EF doesn't know about it and you get error with above line (..not found in the ObjectStateManager..) so you set its state to "Deleted" to inform EF by:
//generate it yourself if not posted from edit/delete view
//var model = new Model { Id = 123 };
//set to delete
db.Entry(model).State = EntityState.Deleted;
db.SaveChanges();
Can you just do this?
var customer = localDb.Customers.Single(o => o.CustomerId == id);
localDb.Customers.Remove(customer);
localDb.SaveChanges();
public T Delete<T>(T obj) where T : class
{
T existing = context.Set<T>().Find(GetKeys(obj, context));
if (existing != null)
{
context.Set<T>().Remove(existing);
context.SaveChanges();
return existing;
}
return null;
}
private object[] GetKeys<T>(T entity, DbContext context) where T : class
{
ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
ObjectSet<T> set = objectContext.CreateObjectSet<T>();
var keyNames = set.EntitySet.ElementType
.KeyMembers
.Select(k => k.Name).ToArray();
Type type = typeof(T);
object[] keys = new object[keyNames.Length];
for (int i = 0; i < keyNames.Length; i++)
{
keys[i] = type.GetProperty(keyNames[i]).GetValue(entity, null);
}
return keys;
}
I know this question is quite old but none of the above worked for me since i was deleting registers from more than one class/service at once and each one of them was instantiating it's own database connection context.
What i did to solve it was to send the first created context to the rest of the classes/services that where going to access the database.
For example, my serviceA
was going to delete some of it's registers and call serviceB
and serviceC
to do the same with their registers.
I would then delete my registers on serviceA
and pass as a parameter the context created on the serviceA
to serviceB
and serviceC
.