C# 使用DevXPress列表框使用实体框架更新实体(多对多关系)
我在两个实体角色和权限之间有多对多关系。 在角色编辑表单中,我有两个列表框,第一个包含所有可用权限,第二个包含角色拥有的权限。我可以在两个列表之间移动项目 当我点击编辑按钮时,我想用第二个列表框中的所有权限更新角色 角色.aspxC# 使用DevXPress列表框使用实体框架更新实体(多对多关系),c#,asp.net,entity-framework,listbox,devexpress,C#,Asp.net,Entity Framework,Listbox,Devexpress,我在两个实体角色和权限之间有多对多关系。 在角色编辑表单中,我有两个列表框,第一个包含所有可用权限,第二个包含角色拥有的权限。我可以在两个列表之间移动项目 当我点击编辑按钮时,我想用第二个列表框中的所有权限更新角色 角色.aspx <table style="width: 100%"> <tr> <td style="width: 35%"> <
<table style="width: 100%">
<tr>
<td style="width: 35%">
<div class="BottomPadding">
<dx:ASPxLabel ID="lblASPxListBox1" runat="server" Text="Droits disponibles:" />
</div>
<dx:ASPxListBox ID="ASPxListBox1" runat="server" KeyFieldName="Id" SelectionMode="Multiple" ClientInstanceName="ASPxListBox1">
<Columns>
<dx:ListBoxColumn FieldName="name" Name="name" />
</Columns>
</dx:ASPxListBox>
</td>
<td style="width: 35%">
<div class="BottomPadding">
<dx:ASPxLabel ID="lblChosen" runat="server" Text="Droits accordés:" />
</div>
<dx:ASPxListBox ID="ASPxListBox2" runat="server" KeyFieldName="Id" SelectionMode="Multiple" ClientInstanceName="ASPxListBox2">
<Columns>
<dx:ListBoxColumn FieldName="name" Name="name" />
</Columns>
</dx:ASPxListBox>
</td>
</tr>
</table>
Roles.aspx.cs
// Update method
Control listBox2Control = grid.FindEditFormTemplateControl("ASPxListBox2");
int selectedId = (int)grid.GetSelectedFieldValues(new string[] { "Id" }).FirstOrDefault();
using(AccountModelContainer context = new AccountModelContainer())
{
Role role = (from r in context.RoleSet
where r.Id == selectedId
select r).FirstOrDefault<Role>();
ASPxListBox listBox = (ASPxListBox)listBox2Control;
List<Right> rights = (List<Right>) (listBox.DataSource);
role.Right = rights;
context.RoleSet.Attach(role);//Error
context.SaveChanges();
}
单击“编辑”按钮时,出现以下错误:
无法定义两个对象之间的关系,因为
它们附着到不同的ObjectContext对象
我应该如何防止此异常