外键属性错误

本文关键字:错误 属性 | 更新日期: 2023-09-27 18:31:32

我有一个出租物业的集合,每个物业都有一堆图像附加到它们(作为子对象)。我正在将 EF 4.0 与 sql ce 数据库一起使用,我需要能够从数据库中删除所有属性和图像。这是我正在使用的代码:

    private void SaveProperty()
    {
        try
        {
            if (PropertyList != null)
            {
                //Purge old database      
                IList<Property> ClearList = new List<Property>(from property in entities.Properties.Include("Images") select property);
                foreach (Property a in ClearList)
                {
                    if (a != null)
                    {
                        if (a.Images.Count != 0)
                        {
                            Property property = entities.Properties.FirstOrDefault();
                            while (property.Images.Count > 0)
                            {
                                var image = property.Images.First();
                                property.Images.Remove(image);
                                entities.DeleteObject(image);
                            }
                            entities.SaveChanges();
                        }
                        entities.DeleteObject(a);
                        entities.SaveChanges();
                    }
                }

                foreach(Property p in PropertyList.ToList())
                {                        
                    //Store sort (current position in list)
                    p.Sort = PropertyList.IndexOf(p);
                    entities.AddToProperties(p);
                    entities.SaveChanges();
                }
            }
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message);
        }
    }

我收到此错误:操作失败:无法更改关系,因为一个或多个外键属性不可为空。对关系进行更改时,相关的外键属性将设置为 null 值。如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。

它直接链接到 Image foreach 循环之后的 SaveChanges() 命令。知道我为什么得到这个吗?

编辑:

这是在我的旧程序结构(没有 mvvm)下运行良好的旧代码。

        private void DeleteProperty()
    {
        if (buttonPres.IsChecked == false)
        {
            //Perform parts of DeleteImage() method to remove any references to images (ensures no FK errors)
            Property p = this.DataContext as Property;
            if (p == null) { return; }
            MessageBoxResult result = System.Windows.MessageBox.Show(string.Format("Are you sure you want to delete property '{0}'?'nThis action cannot be undone.", p.SaleTitle), "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
                try
                {
                    int max = listBoxImages.Items.Count;
                    for (int i = 0; i < max; i++)
                    {
                        Image img = (Image)listBoxImages.Items[0];
                        entities.DeleteObject(img);
                        entities.SaveChanges();
                    }
                    entities.DeleteObject(p);
                    entities.SaveChanges();
                    BindData();
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
                }
        }
    }

注意:Binddata() 只是刷新属性列表框。

外键属性错误

我的赌注是在删除图像后删除保存更改,如果此关系为 1-多,则将更改保存到违反此关系的状态

尝试:

       //Purge old database                  
    foreach (Property a in entities.Properties)
    {
        if (a != null)
        {
            if (a.Images != null)
            {
                foreach (Image i in a.Images)
                {
                    entities.Images.DeleteObject(i);
                }
            }
            entities.Properties.DeleteObject(a);
        }
    }
    entities.SaveChanges();