0

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

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

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

它在 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() 只是刷新属性列表框。

4

1 回答 1

0

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

尝试:

       //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();
于 2012-04-18T11:06:26.420 回答