[英]Entity Framework context issue
好吧...实体框架6的所有问题。
我正在使用EF编写一个小型的Intranet门户。 这是一个使用gridview的非常简单的CRUD。 一切都很顺利,但尝试保存更改时遇到问题。
为了在gridview上进行分页,排序和过滤,我创建了一个包含实体对象列表的会话变量,然后将该会话变量绑定到gridview。
//Class level variables
private nsdc_supplyEntities _context = new nsdc_supplyEntities();
private VlanClass _vc;
private EnvironmentTypesClass _ec;
protected void Page_Load(object sender, EventArgs e)
{
_vc = new VlanClass(_context);
_ec = new EnvironmentTypesClass(_context);
if (!IsPostBack)
{
Session["vlans"] = _vc.GetAllVlans();
BindData();
}
}
protected void BindData()
{
GridView1.DataSource = Session["vlans"];
GridView1.DataBind();
}
protected void GridView1_EditVlan(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
这样,如果用户将列表过滤到较小的结果集或进行排序和页面排序,然后执行触发回发的其他事件,则Gridview的集合将保持不变。
但是据我所知,Session [“ vlans”]中的VLAN列表不再具有上下文(由于回发事件?)。 因此,当我尝试编辑所选vlan并将其保存时(尤其是当我尝试将vlane_type子实体添加到vlan实体时),我会收到错误消息。
我尝试过将vlan条目重新附加到_context中,但这带来了整个“另一个蜡球”。 做这个的最好方式是什么?
这是我目前的更新:
protected void UpdateVlan(object sender, GridViewUpdateEventArgs e)
{
CheckBoxList environmenttypesCBL = (GridView1.Rows[e.RowIndex].FindControl("EnvironmenttypesCBL") as CheckBoxList);
int vlanid = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblid")).Text);
//At first I was creating a new instance of vlan in question and saving that,
//but then database and the Session["vlan"] variable (and gridview) is out of sync
//vlan editedVlan = new vlan();
//editedVlan = _vc.GetVlanByID(vlanid);
//Get the vlan I am editing from Session["vlan"]
List<vlan> list = Session["vlans"] as List<vlan>;
vlan editedVlan = list.Find(i => i.id == vlanid);
//Tried reattaching but "The object cannot be detached because it is not attached to the ObjectStateManager."
//_context.Attach(editedVlan);
#region EnvironmentTypes
editedVlan.environment_type.Clear();
if (environmenttypesCBL.Items.Count > 0)
{
foreach (ListItem l in environmenttypesCBL.Items.Cast<ListItem>())
{
//tried adding an environment_type to the editedVlan but "The object cannot be detached because it is not attached to the ObjectStateManager."
if (l.Selected)
editedVlan.environment_type.Add(_ec.GetEnvironmentTypeByID(Convert.ToInt32(l.Value)));
}
}
#endregion
_vc.UpdateSelectedVlan();
GridView1.EditIndex = -1;
BindData();
}
每次POST或GET都会创建_context,因此,VLAN列表将绑定到不同的上下文。
会话不关心POST或GET,我认为您应该更改此设置:
_vc = new VlanClass(_context);
_ec = new EnvironmentTypesClass(_context);
if (!IsPostBack)
{
Session["vlans"] = _vc.GetAllVlans();
BindData();
}
对此:
if (Session["vlans"] == null )
{
_context = new nsdc_supplyEntities(); // only create the context here not in the instance var declaration.
_vc = new VlanClass(_context);
_ec = new EnvironmentTypesClass(_context);
Session["vc"] = _vc;
Session["vlans"] = _vc.GetAllVlans();
Session["context"] = _context;
}
else
{
_context = (DbContext)Session["context"]; // cast to your Context
_vc = (VlanClass) Session["vc"] ;
}
if (!IsPostBack)
{
BindData();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.