[英]Why is my code triggering multiple DB context though I'm only using 1 context
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. IEntityChangeTracker的多个实例不能引用一个实体对象。
Below is the line that triggers it 以下是触发它的行
Line 163: foreach (ligne l in SessionVariables.ligneNouvelleFacture) 第163行:foreach(SessionVariables.ligneNouvelleFacture中的ligne)
Line 164: { 第164行:{
Line 165: dc.ligne.Add(l); 第165行:dc.ligne.Add(l);
Line 166: } 第166行:}
So what I'm simply trying to do is to use EntityFramework context class default methods to insert data in the DB. 因此,我要做的只是使用EntityFramework上下文类默认方法在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()
数据存储在对象的静态列表中,我使用foreach
进行迭代,然后在保存更改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. 我不知道为什么会收到该错误,因为我仅在此Webform中的代码背后创建了上下文的单个实例。 And that same context varible is used to fill a gridview on page load. 并且该上下文变量用于在页面加载时填充gridview。
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 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. 在下面的提示之后,以及在MSDN上看到Dispose
并阅读了Entity Framework和context dispose之后 ,我尝试了一种似乎可行的新方法。 Hopefully it's not too bad. 希望它还不错。 I maybe wrongly implementing using
but it seems it does not dispose
implicitly. 我可能会错误地实现using
但似乎并未隐式dispose
。
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 希望这可以帮助某人
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? 为什么需要再次将所有对象而不是新对象添加到上下文中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.