开发者

problem of cascade delete in entity framework

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">
    开发者_如何学Python  <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>


<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.

problem of cascade delete in entity framework

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜