-1

を開発していasp.net Web Forms application with Entity Frameworkます。テーブルPROVINCE_ID内の既存のデータから更新しようとすると(以前に 1 つのエントリで選択したものを変更して更新しようとしました)、以下のエラーが発生しました。DISTRICTPROVINCE_ID

A referential integrity constraint violation occurred: A primary key property that is a part of referential integrity constraint cannot be changed when the dependent object is Unchanged unless it is being set to the association's principal object. The principal object must be tracked and not marked for deletion.

テーブル構造は以下です。

ここに画像の説明を入力

更新しました

aspx ページのコード。

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <asp:EntityDataSource ID="District_EDS" runat="server" ConnectionString="name=ABC_DBEntities"
        DefaultContainerName="ABC_DBEntities" EnableFlattening="False" EntitySetName="DISTRICT"
        EnableUpdate="True" Where="it.DISTRICT_ID = @DISTRICT_ID">
        <WhereParameters>
            <asp:QueryStringParameter DbType="String" Name="DISTRICT_ID" QueryStringField="DISTRICT_ID" />
        </WhereParameters>
    </asp:EntityDataSource>
    <%--DropDownList Entity Data Source--%>
    <asp:EntityDataSource ID="Province_EDS" runat="server" ConnectionString="name=ABC_DBEntities"
        DefaultContainerName="ABC_DBEntities" EnableFlattening="False" EntitySetName="PROVINCE"
        EnableUpdate="True">
    </asp:EntityDataSource>
    <asp:DetailsView ID="District_DV" runat="server" AutoGenerateRows="False" DefaultMode="Edit"
        DataKeyNames="DISTRICT_ID,PROVINCE_ID" DataSourceID="District_EDS" OnItemCommand="District_DV_ItemCommand">
        <Fields>
            <asp:TemplateField HeaderText="District Id *" SortExpression="DISTRICT_ID">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("DISTRICT_ID") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("DISTRICT_ID") %>'></asp:Label>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("DISTRICT_ID") %>'></asp:Label>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Province Name *" SortExpression="PROVINCE_ID">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("PROVINCE_ID") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddl_PROVINCE_id" Width="200" runat="server" DataSourceID="Province_EDS"
                        DataTextField="PROVINCE_NAME" DataValueField="PROVINCE_ID" SelectedValue='<%# Bind("PROVINCE_ID") %>'>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="PROVINCE_ID_VAL" runat="server" ControlToValidate="ddl_PROVINCE_id"
                        ErrorMessage="Province Name is required." ValidationGroup="DISTRICT"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("PROVINCE_ID") %>'></asp:Label>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="District Name *" SortExpression="DISTRICT_NAME">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("DISTRICT_NAME") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txt_DISTRICT_name" Width="200" runat="server" Text='<%# Bind("DISTRICT_NAME") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="DISTRICT_NAME_VAL" runat="server" ControlToValidate="txt_DISTRICT_name"
                        ErrorMessage="District Name is required." ValidationGroup="DISTRICT"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DISTRICT_NAME") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="true" ShowCancelButton="false" ValidationGroup="DISTRICT" />
        </Fields>
    </asp:DetailsView>
</asp:Content>

コードビハインド

  // Return to index page after updating content on the page.
    protected void District_DV_ItemCommand(Object sender, DetailsViewCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            Response.Redirect("~/District/Index.aspx", false);

        }
    }

これをどのようにクラウドで解決しますか?

4

1 に答える 1

0

たぶん、これはいくつかのガイダンスになるかもしれません: https://code.msdn.microsoft.com/ASPNET-MVC-Application-b01a9fe8/sourcecode?fileId=134759&pathId=2009908151

private void UpdateInstructorCourses(string[] selectedCourses, Instructor instructorToUpdate) 
    { 
        if (selectedCourses == null) 
        { 
            instructorToUpdate.Courses = new List<Course>(); 
            return; 
        } 

        var selectedCoursesHS = new HashSet<string>(selectedCourses); 
        var instructorCourses = new HashSet<int> 
            (instructorToUpdate.Courses.Select(c => c.CourseID)); 
        foreach (var course in db.Courses) 
        { 
            if (selectedCoursesHS.Contains(course.CourseID.ToString())) 
            { 
                if (!instructorCourses.Contains(course.CourseID)) 
                { 
                    instructorToUpdate.Courses.Add(course); 
                } 
            } 
            else 
            { 
                if (instructorCourses.Contains(course.CourseID)) 
                { 
                    instructorToUpdate.Courses.Remove(course); 
                } 
            } 
        } 
    } 
于 2015-09-21T07:53:24.303 に答える