语句与REFERENCE约束冲突

时间:2011-07-27 15:03:08

标签: c# sql

按钮点击代码

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功能:

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;
     }
}

我正在

  

DELETE语句与REFERENCE约束冲突   “FK_Channel_Folder”。冲突发生在数据库中   “C:\ USERS \ ADMIN \ DOCUMENTS \ VISUAL STUDIO 2010 \ PROJECTS \ PROJ RSS \ PROJ   RSS \ BIN \ DEBUG \ RSS.MDF“,表”dbo.Channel“,列'FolderID'。   声明已被终止。

1 个答案:

答案 0 :(得分:2)

如果您尝试删除的项目已被删除,则会有违反的外键引用。看起来ChannelFolder个依赖它的实体。

在删除Folder或设置级联删除之前,您需要删除所有相关的Channel实体。