实体框架级联删除问题

发布于 2024-11-17 01:59:25 字数 1290 浏览 3 评论 0原文

根据级联删除,我编写了以下代码,但出现错误: 更新条目时发生错误。有关详细信息,请参阅内部异常。

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2024-11-24 01:59:25
<OnDelete Action="Cascade" />

这仅涵盖对象上下文中加载的实体(它将级联删除的相关实体),但不涵盖数据库本身。确保您已在数据库上为相应的表设置级联删除约束。

编辑:

要在SQL Server中设置级联删除,请将外键关系的删除规则设置为级联。为此,打开 SQL Server Management Studio,打开相关表进行设计并显示外键关系。在 INSERT 和 UPDATE 规范中将级联设置为删除规则。

在此处输入图像描述

<OnDelete Action="Cascade" />

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.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文