实体框架CheckboxList数据库插入

时间:2013-07-31 21:33:02

标签: entity-framework foreign-keys checkboxlist

更新:

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

感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分: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
                        }
                }
            }
        }
    }