0

更新:

我正在使用 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 关系。

感谢您提供的任何帮助!

4

1 回答 1

0

下面的代码有效。我已经移动了我的 context.savechanges(); 在循环之外。我的问题发生在我的 ItemInserted 命令上。

protected void AddPrincipleStaffDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        CheckBoxList PrincipleStaffCheckBox = (CheckBoxList)AddPrincipleStaffDetailsView.FindControl("PrincipleStaffTitleCheckBoxList");

        if (PrincipleStaffCheckBox.Items.Count > 0)
        {
            foreach (ListItem item in PrincipleStaffCheckBox.Items)
            {
                if (item.Selected)
                {

                    string stringSession = Session["ProductionID"].ToString();
                    int intProductionID = 0;
                    intProductionID = Int32.Parse(stringSession);
                    var principlestafftitle = item.Value;

                        try
                        {
                            using (var context = new FactoryTheaterModelFirstContainer())
                            {

                                Production proid = context.Productions.Single(i => i.proProductionID == intProductionID);
                                PrincipleStaff principlestaff = new PrincipleStaff()
                                {
                                    PrincipleStaffTitle = principlestafftitle,
                                    Production_proProductionID = intProductionID
                                };

                                proid.PrincipleStaffs.Add(principlestaff);
                                context.SaveChanges();
                            }
                        }
                        catch (Exception f)
                        {
                            Console.WriteLine(f); // or log to file, etc.
                            throw; // re-throw the exception if you want it to continue up the stack
                        }
                }
            }
        }
    }
于 2013-08-02T21:21:18.010 回答