外键属性错误

时间:2012-04-18 09:04:12

标签: c# wpf entity-framework entities

我有一组租赁属性,每个属性都附有一堆图像(作为子对象)。我使用带有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()只是刷新属性列表框。

1 个答案:

答案 0 :(得分: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();