语句与REFERENCE约束冲突

本文关键字:冲突 约束 REFERENCE 语句 | 更新日期: 2023-09-27 18:03:48

按钮点击代码

bindingSource1.EndEdit();
            try
            {
                // Delete all the Channels for the current Folder.
                int folderID = (int)RemoveFolderBox.SelectedValue;
                deleteChannels(folderID);
                // Delete the folder itself.
                RSSDataSet1.FolderRow folder = rSSDataSet1.Folder.FindByFolderID(folderID);
                folder.Delete();
                int rowsAffected = folderTableAdapter.Update(rSSDataSet1);
                if (rowsAffected > 0)
                {
                    //statusLabel.Text = "Folder successfully deleted.";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem when deleting folder:" + ex.Message);
            }

deleteChannels-function:

private void deleteChannels(int folderID)
{
    try
    {
       RSSDataSet1.ChannelRow[] channels = (RSSDataSet1.ChannelRow[])rSSDataSet1.Channel.Select("FolderID = " + folderID.ToString());
       foreach (RSSDataSet1.ChannelRow channel in channels)
       {
          int channelID = channel.ChannelID;
          channel.Delete();
       }
       int rowsAffected = channelTableAdapter1.Update(rSSDataSet1);
     }
     catch (Exception ex)
     {
       throw ex;
     }
}

I'm getting

DELETE语句与REFERENCE约束冲突"FK_Channel_Folder"。数据库发生冲突"c:' users ' admin ' documents ' visual studio 2010' projects ' proj rss ' proj"RSS ' BIN '调试' RSS。MDF",表"dbo。通道",列"FolderID"。的语句已被终止。

语句与REFERENCE约束冲突

您有一个外键引用,如果您试图删除的项被删除,该外键引用将被违反。看起来ChannelFolder实体依赖于它。

您需要在删除Channel之前删除任何依赖的Folder实体,或者设置级联删除。