0

I am having trouble when deleting a relationship from entity framework when I have declared the relationship as a split table.

Basically I need to have some code that will check if the entity was loaded by the same context. The check below does not work.

var data = new ProductItemData() { Id = pv.ProductItems[i].Id };

if (m_Context.ProductData.Where(w => w.Id == data.Id).Count() == 0)
{
   m_Context.ProductData.Attach(data);
}

m_Context.ProductData.Remove(data); 

When its not loaded in the same context, i.e data has been lazy loaded from database, I rightly get this error

The object cannot be deleted because it was not found in the ObjectStateManager.

And when the item is in the same context and the attach() method is executed. I get this error

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

Does anyone have an idea on a check I can do?

I think it ignores any checks I do..It removes it fine when I reload the context but on the same context, it always goes to the attach code

Thanks

2 Answers 2

0

If you use .ToList then you force the run time to activate the sql query (the eager way).

Try:

List<ProductData> data = m_Context.ProductData.Where(w => w.Id == data.Id).ToList();

if (data.Count() == 0)
{
   m_Context.ProductData.Attach(data);
}

m_Context.ProductData.Remove(data); 

And instead of attaching you can try the following (taken from here):

context.Entry(oldEntity).CurrentValues.SetValues(newEntity);
0

If you would like to check the state of an entity for a given entity then use the change tracker:

if(m_Context.Entry(someEntity).State == EntityState.Detached)
{
    // entity was not added by this context or it is brand new and not added to a dbset
}

Here are the different values for EntityState:

  • Added: The entity is being tracked by the context but does not yet exist in the database.
  • Deleted: The entity is being tracked by the context and exists in the database, but has been marked for deletion from the database the next time SaveChanges is called.
  • Detached: The entity is not being tracked by the context. An entity is in this state immediately after it has been created with the new operator or with one of the DbSet Create methods.
  • Modified: The entity is being tracked by the context and exists in the database, and some or all of its property values have been modified.
  • Unchanged: The entity is being tracked by the context and exists in the database, and its property values have not changed from the values in the database.
1
  • Thanks, this makes sense, however, I think it ignores any checks I do (The code above complies OK) It removes it fine when I reload the context but when on the same context, it always goes to the attach code and then errors. Jun 16, 2014 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.