我正在开发一个asp.net Web Forms application with Entity Framework
. 当我尝试PROVINCE_ID
从表中的现有数据进行更新时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);
}
}
云我如何解决这个问题?