How can I delete items in one context from another in Entity Framework?

1

EssaieFilament is the name of a table in my database.

note: I translated the error msg from french, so its might not be exact.

Hello, I fill up a list of "EssaieFilament", then send the list to a function that delet them but as the context change, it's give me a error.

L'exception System.InvalidOperationException happened
HResult=0x80131509 Message=Impossible to delet the object, as its can't be found in ObjectStateManager. Source=EntityFramework

Is there a ways to fix this without actually have to send the context?

public static void SupprimerEssaieFilament(List<EssaieFilament> essaieFilament_list)
{
    using (var context = new tp2_1608469Entities1())
    {
        foreach (EssaieFilament essaieFilament in essaieFilament_list)
        {
            context.EssaieFilaments.Remove(essaieFilament);
            context.SaveChanges();
        }
    }
}
c#
entity-framework
asked on Stack Overflow Apr 26, 2018 by MaxyArthes • edited Apr 26, 2018 by MaxyArthes

3 Answers

0

It will not work like this. You are opening a new context (connection to the DataBase), but the entities that you have retrieved belong to another connection (the one in the first method, where you have queried them).

You should either send the context to the method, or send a list of Id's, and when you open the new context, retrieve the entities, and then delete (or whatever you want to do) them.

answered on Stack Overflow Apr 26, 2018 by m3n7alsnak3
0

You could do something with .AttachTo in this situation, where you attach your list of entities to the new context and then delete them.

You can also re-query the new context and delete the found items. This will perform much better than the code in your example, since you delete all items at once in a single call, instead of once-per-item in your collection.

public static void SupprimerEssaieFilament(List<EssaieFilament> essaieFilament_list)
{
    using (var context = new tp2_1608469Entities1())
    {
        var itemIds = essaieFilament_list.Select(x=>x.Id);
        var itemsToDelete = context.EssaieFilaments.Where(x=>itemsIds.Contains(x.Id));

        context.EssaieFilaments.RemoveRange(itemsToDelete);
        context.SaveChanges();
    }
}
answered on Stack Overflow Apr 26, 2018 by Daniel Szabo • edited Apr 26, 2018 by Daniel Szabo
-1

If the context changes, you have to check if the essaieFilament object you want to delete exists in the context before delete it... So try this:

public static void SupprimerEssaieFilament(List<EssaieFilament> essaieFilament_list)
{
    using (var context = new tp2_1608469Entities1())
    {
        foreach (EssaieFilament essaieFilament in essaieFilament_list)
        {
            if (context.EssaieFilaments.Contains(essaieFilament))
            {
                context.EssaieFilaments.Remove(essaieFilament);
                context.SaveChanges();
            }
        }
    }
}
answered on Stack Overflow Apr 26, 2018 by na2axl

User contributions licensed under CC BY-SA 3.0