日期:2014-05-17  浏览次数:20604 次

ObjectContext 实例已释放,不可再用于需要连接的操作。
protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
  LoadUserDropDownList();
  }
}
3.Page_Load方法中的LoadUserDropDownList方法代码如下:
private void LoadUserDropDownList()
  {
  using (OrderDBContainer db = new OrderDBContainer())
  {
  ddlUsers.DataSource = from u in db.UserAccounts
  orderby u.LastName
  select new { Name = u.LastName + ", " + u.FirstName, Id = u.Id };

  ddlUsers.DataTextField = "Name";
  ddlUsers.DataValueField = "Id";
  ddlUsers.DataBind();//////////////ObjectContext 实例已释放,不可再用于需要连接的操作。

  ddlUsers.Items.Insert(0, new ListItem("Create New User", ""));
  }
  }
当运行到DataBind()时候就出现如题
我想了一个晚上,想不明白,这为什么?是不是VS2010中文版BUG
使用Entity Framework例子

------解决方案--------------------
ddlUsers.DataSource = from u in db.UserAccounts
orderby u.LastName
select new { Name = u.LastName + ", " + u.FirstName, Id = u.Id };

=>

ddlUsers.DataSource = (from u in db.UserAccounts
orderby u.LastName
select new { Name = u.LastName + ", " + u.FirstName, Id = u.Id }).ToList();