を開発していasp.net Web Forms application with Entity Framework
ます。テーブルPROVINCE_ID
内の既存のデータから更新しようとすると(以前に 1 つのエントリで選択したものを変更して更新しようとしました)、以下のエラーが発生しました。DISTRICT
PROVINCE_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);
}
}
これをどのようにクラウドで解決しますか?