0

Sorry if the code is a bit long. I only have one context. But I get the following error

An entity object cannot be referenced by multiple instances of IEntityChangeTracker.

Below is the line that triggers it

Line 163: foreach (ligne l in SessionVariables.ligneNouvelleFacture)

Line 164: {

Line 165: dc.ligne.Add(l);

Line 166: }

So what I'm simply trying to do is to use EntityFramework context class default methods to insert data in the DB. The data is stored in a static list of object that I iterate with a foreach and add then in the context before saving the change dc.SaveChanges()

I don't know why I get that error since I have only create a single instance of the context in this webform behind code. And that same context varible is used to fill a gridview on page load.

Can you help me figure it out ? Below is my code

public class Utilities()
{
   public static list<object> myList = new list<object>();
}

public partial classe MyWebform : Page
{
   MyContext cnx = new MyContext();
   potected void Page_Load()
   { // Do something with the context "cnx" and static list of utilities class}

protected void button_Click(...)
{
   //Also do something with the context "cnx" and the static list of utilities class
}
}

UPDATE

After the anwser below and also after seeing Dispose on MSDN and reading Entity Framework and context dispose, I tried a new approach which seems to be working. Hopefully it's not too bad. I maybe wrongly implementing using but it seems it does not dispose implicitly.

public class  MyWebForm .....
{
  // I do no declare a context variable object so that every time a context must be used, I do the following.

  using (MyContext cnx = new MyContext())
  {
     // ...do something with cnx
     cnx.Dispose();  // This line appears to be compulsory to avoid multiple context error
  }
}

Hopefully this helps someone

Community
  • 1
  • 1
Kurt Miller
  • 567
  • 1
  • 8
  • 23
  • After your update: `cnx.Dispose();` is redundant: the end of the `using` does that. – Gert Arnold Mar 25 '17 at 12:14
  • @GertArnold Yeah that is in theory true. BUT I don't know why, but when I go back and forth between webforms, it says "multiple context error" unless I place a `Dispose` (this happened for one specific method only) – Kurt Miller Mar 25 '17 at 12:21

1 Answers1

0

You are creating multiple contexts. Every time a page is loaded a new one is created. You keep a static list of objects and then try to add them again and again to a context, even when they already are in one. This will cause an error.

This may also cause a memory leak since every object has a separate context which cannot be released due to the objects being alive. You should redesign the functionality. Is it necessary to keep the objects in memory? Why do you need to add all objects into the context again and not just the new ones?

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • Because I am a beginner a webform and C#. I do not master advanced topics like postback, context, etc .... Seeing my code what would a better approach ? – Kurt Miller Mar 25 '17 at 06:56
  • How cant I circumvent this ? Creating a static object for my context in a "utilitties" class seems to make the application go in infinite loop on the culprit line (this time at least it does not say there is a multiple context issue) – Kurt Miller Mar 25 '17 at 07:03
  • Or maybe after reading https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.modeling.context.dispose.aspx, how can I implement it dispose the context when before the page is "close" – Kurt Miller Mar 25 '17 at 07:09