更新:
我正在使用 Detailsview 插入控件,并尝试从 CheckBoxList 中的复选框在数据库中创建新行。我收到“INSERT 语句与 FOREIGN KEY 约束冲突”
这是我的cs:
protected void AddPrincipleStaffDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
CheckBoxList PrincipleStaffTitle = (CheckBoxList)FindControl("PrincipleStaffTitle");
if (PrincipleStaffTitle != null)
{
foreach (ListItem item in PrincipleStaffTitle.Items)
{
if (item.Selected)
{
string stringSession = Session["ProductionID"].ToString();
int intProductionID = Int32.Parse(stringSession);
var ID = item.Value;
using (var context = new FactoryTheaterModelFirstContainer())
{
PrincipleStaff principlestaff = new PrincipleStaff();
principlestaff.PrincipleStaffTitle = ID;
principlestaff.Production_proProductionID = intProductionID;
context.PrincipleStaffs.Add(principlestaff);
context.SaveChanges();
}
}
}
}
这是aspx:
<asp:EntityDataSource ID="PrincipleStaffSource" runat="server" ConnectionString="name=FactoryTheaterModelFirstContainer" DefaultContainerName="FactoryTheaterModelFirstContainer" EnableFlattening="False" EntitySetName="PrincipleStaffs" EntityTypeFilter="PrincipleStaff" EnableInsert="True"></asp:EntityDataSource>
<asp:DetailsView ID="AddPrincipleStaffDetailsView" runat="server" AutoGenerateRows="False" DataKeyNames="PrincipleStaffID" DataSourceID="PrincipleStaffSource" Height="50px" Width="730px" DefaultMode="Insert" OnItemInserting="AddPrincipleStaffDetailsView_ItemInserting" OnItemInserted="AddPrincipleStaffDetailsView_ItemInserted">
<Fields>
<asp:TemplateField HeaderText="Add Principle Staff Role:" SortExpression="PrincipleStaffTitle">
<InsertItemTemplate>
<asp:CheckBoxList ID="PrincipleStaffTitle" runat="server" SelectedValue='<%# Bind("PrincipleStaffTitle") %>'>
<asp:ListItem Value="Director">Director(s)</asp:ListItem>
<asp:ListItem Value="Assistant Director">Assistant Director(s)</asp:ListItem>
<asp:ListItem Value="Written By">Written By(s)</asp:ListItem>
<asp:ListItem Value="Executive Producer">Executive Producer(s)</asp:ListItem>
<asp:ListItem Value="Producer">Producer(s)</asp:ListItem>
<asp:ListItem Value="Techincal Director">Technical Director(s)</asp:ListItem>
</asp:CheckBoxList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PrincipleStaffTitle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
Production.proProductionID 是我试图进入 PrincipleStaff Production_proProductionid 列的外键。我似乎无法创建 FK 关系。
感谢您提供的任何帮助!