实体框架级联删除问题
根据级联删除,我编写了以下代码,但出现错误: 更新条目时发生错误。有关详细信息,请参阅内部异常。
using (doctorEntities de = new doctorEntities())
{
var delete_base_print = (from Table_infobase_print tip in de.Table_infobase_print
where tip.ID == ((Doctor.Table_infobase_print)(datagrid_table_infobase_print.SelectedItem)).ID
select tip).First();
de.DeleteObject(delete_base_print);
de.SaveChanges();
}
<Association Name="FK_Table_infodetail_print_Table_infobase_print">
<End Role="Table_infobase_print" Type="doctorModel.Table_infobase_print" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
<End Role="Table_infodetail_print" Type="doctorModel.Table_infodetail_print" Multiplicity="*" >
</End>
<ReferentialConstraint>
<Principal Role="Table_infobase_print">
<PropertyRef Name="ID" />
</Principal>
<Dependent Role="Table_infodetail_print">
<PropertyRef Name="ID_infobase" />
</Dependent>
</ReferentialConstraint>
</Association>
According to cascade deleting, i wrote below codes but there is an error :
An error occurred while updating the entries. See the inner exception for details.
using (doctorEntities de = new doctorEntities())
{
var delete_base_print = (from Table_infobase_print tip in de.Table_infobase_print
where tip.ID == ((Doctor.Table_infobase_print)(datagrid_table_infobase_print.SelectedItem)).ID
select tip).First();
de.DeleteObject(delete_base_print);
de.SaveChanges();
}
<Association Name="FK_Table_infodetail_print_Table_infobase_print">
<End Role="Table_infobase_print" Type="doctorModel.Table_infobase_print" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
<End Role="Table_infodetail_print" Type="doctorModel.Table_infodetail_print" Multiplicity="*" >
</End>
<ReferentialConstraint>
<Principal Role="Table_infobase_print">
<PropertyRef Name="ID" />
</Principal>
<Dependent Role="Table_infodetail_print">
<PropertyRef Name="ID_infobase" />
</Dependent>
</ReferentialConstraint>
</Association>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这仅涵盖对象上下文中加载的实体(它将级联删除的相关实体),但不涵盖数据库本身。确保您已在数据库上为相应的表设置级联删除约束。
编辑:
要在SQL Server中设置级联删除,请将外键关系的删除规则设置为级联。为此,打开 SQL Server Management Studio,打开相关表进行设计并显示外键关系。在 INSERT 和 UPDATE 规范中将级联设置为删除规则。
This only covers the loaded entities in your object context (it will cascade deleted related entities), but not the database itself. Make sure you have set up cascaded delete constraint on the database for the corresponding table.
Edit:
To set cascading deletes in SQL Server, set the delete rule for the foreign key relationship to cascade. For this open up SQL Server Management Studio, open up the table in question for design and show the foreign key relationship. Set Cascade as Delete Rule within the INSERT and UPDATE specification.