C#-ObjectContext实例已被处置,不能再用于需要连接的操作 [英] C# - The ObjectContext instance has been disposed and can no longer be used for operations that require a connection
问题描述
I'm sorry for the repetition. I'm heading with a same problem but still could not handle it. I'm using Angularjs framework and Asp.net mvc.
我的人物班级:
public partial class Person
{
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Telephone { get; set; }
public string Adress { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
}
我的用户类别:
public partial class User
{
public User()
{
this.People = new HashSet<Person>();
}
public int UserID { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public string Gender { get; set; }
public virtual ICollection<Person> People { get; set; }
}
我的js代码:
$http.get('/Data/GetPeople', { params: { UserID: "1" } }).success(function (data) {
$scope.model = data;
});
我正在尝试从数据库中获取记录:
I'm trying to get records from my database:
public ActionResult GetPeople(int UserID)
{
using (PersonEntities dc = new PersonEntities())
{
var model = new PersonIndexVM();
model.People = dc.People.Where(b => b.UserID == UserID).ToList();
//model.People = dc.People.Include("User").ToList();
return Json(model, JsonRequestBehavior.AllowGet);
}
}
正如我在调试中看到的那样,我正在GetPeople方法中从数据库中获取正确的对象.但是在那之后,我得到了这个错误:
As I see with debugging, I'm getting the right objects from database in GetPeople method. But after that I'm getting this error:
'ObjectContext实例已被处置,不能再用于需要连接的操作.'
'The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.'
我尝试急切加载:model.People = dc.People.Include("User").Where(b => b.UserID == UserID).ToList();
仍然遇到相同的错误.
I tried to Eagerly load: model.People = dc.People.Include("User").Where(b => b.UserID == UserID).ToList();
Still getting the same error.
如果您能帮助我,那将是一种荣幸.
It would be a pleasure if you help me.
推荐答案
问题已解决.我从朋友那里得到帮助.它关于未封闭的连接.我的错.我没有提到.
The problem is solved. I get help from my friend. Its about unclosed connection. Its my fault. I didn't mention it.
在PersonIndexVM()
中,我创建了People = new List<Person>();
Person类是由实体框架创建的.与数据库有关.当在GetPeople()
方法中创建作为PersonIndexVM()
对象的model
并将此model
对象作为json返回时,模型对象尝试在连接关闭后尝试获取用户类信息.我得到这个错误.解决此问题的方法:
In PersonIndexVM()
, I created People = new List<Person>();
Person class is created by entity framework. It is related with database. When I create a model
that is an object of PersonIndexVM()
in GetPeople()
method and return this model
object as a json, model object try to reach User class informations after the connection closed. And I'm getting this error. To solve this problem:
-
关闭延迟加载以防止获取用户信息.
dc.Configuration.LazyLoadingEnabled = false;
创建另一个与数据库无关的类,并将其对象作为Json返回.
Creating another class not related with database and return its object as Json.
这篇关于C#-ObjectContext实例已被处置,不能再用于需要连接的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!