我正在开发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);
}
}
我如何解决这个问题?
答案 0 :(得分:0)
也许这可能是一些指导: https://code.msdn.microsoft.com/ASPNET-MVC-Application-b01a9fe8/sourcecode?fileId=134759&pathId=2009908151
:checked + .check-sq:before {
animation: nameyouranimation [duration] forwards;
}
@keyframes nameyouranimation {
0% {
transform: scale(0);
}
90% {
transform: scale(1.3);
}
100% {
transform: scale(1);
}
}