Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 外键属性错误_C#_Wpf_Entity Framework_Entities - Fatal编程技术网

C# 外键属性错误

C# 外键属性错误,c#,wpf,entity-framework,entities,C#,Wpf,Entity Framework,Entities,我有一个租赁物业的集合,每个都有一组图片作为子对象附加到它们上。我将EF4.0与sql ce数据库一起使用,我需要能够从数据库中删除所有属性和图像。以下是我正在使用的代码: private void SaveProperty() { try { if (PropertyList != null) { //Purge old database

我有一个租赁物业的集合,每个都有一组图片作为子对象附加到它们上。我将EF4.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-多,则将更改保存到违反此关系的状态

尝试:


你能在这里添加实体类的代码吗?它可能只是两个简单实体类的副本,一个用于属性,一个用于图像。一个属性可以有1*多个图像。每个图像只有一个属性。要创建属性,只需创建其entities.property.addnew属性;对于图像,我执行CurrentProperty.images.Addnew image。图像属性可以设置为null,因此我认为这不是问题所在,即:关系是该属性可以有多个图像,也可以没有图像。很抱歉,我之前没有说清楚。这是我整个方法的代码。基本上,我是从viewmodel与数据库通信的,因为程序不够大,无法保证有一个单独的数据层。此命令的目的只是删除数据库中的所有现有数据,并使用observablecollection中的数据重新填充它。当我将图像添加到属性并将其保存到数据库,但随后删除该属性并保存更改时,它在同一点失败。我还为我的方法添加了代码,该方法在不使用mvvm的旧模型下执行了相同的操作。
       //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();