0

I have this method :

 public async void Delete(params T[] items) { using (var context = new DataEntities()) { foreach (T item in items) { context.Entry(item).State = System.Data.Entity.EntityState.Deleted; } await context.SaveChangesAsync(); } } 

When I tried to delete an item :

dpc_participant part = (await SimpleIoc.Default.GetInstance<ICrud<dpc_participant>>().Search(x => x.dpc_id_participant_pk == currentitem.dpc_id_participant_pk && x.dpc_id_fk == currentitem.dpc_id_pk)).FirstOrDefault(); SimpleIoc.Default.GetInstance<ICrud<dpc_participant>>().Delete(part); 

I got this error

entity object cannot be referenced by multiple instances of IEntityChangeTracker.

Questions:

  1. What is the reason of this error?
  2. How can I fix it?
1
  • 1
    your parameters came from a different Database Context, than the one you're creating here. Disable proxies on the source context and see if that does the trick. Commented Feb 27, 2015 at 17:35

1 Answer 1

1

This is happening because your items variable is connected to a different context than the context your function is using.

To Fix, either pass the original context you retrieved the items with into this function and use it or re-query them using the new context within the function and then delete.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.