Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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# 无法删除该对象,因为在ObjectStateManager 2中找不到它_C#_.net_Wpf_Database_Entity Framework - Fatal编程技术网

C# 无法删除该对象,因为在ObjectStateManager 2中找不到它

C# 无法删除该对象,因为在ObjectStateManager 2中找不到它,c#,.net,wpf,database,entity-framework,C#,.net,Wpf,Database,Entity Framework,我在截图中看到了问题: 以下是程序开头的代码: public MainWindow() { InitializeComponent(); BlackoutDates(); variables.date = Convert.ToDateTime(MainCalendar.DisplayDate); DateOutput.Content = MainCalendar.DisplayDate.DayOfWeek +

我在截图中看到了问题:

以下是程序开头的代码:

    public MainWindow()
    {
        InitializeComponent();


        BlackoutDates();

        variables.date = Convert.ToDateTime(MainCalendar.DisplayDate);
        DateOutput.Content = MainCalendar.DisplayDate.DayOfWeek + ", " + MainCalendar.DisplayDate.ToShortDateString();



    }

    //initialises entities
    WpfApplication7.AllensCroftEntities1 allensCroftEntities1 = new WpfApplication7.AllensCroftEntities1();
    private Booking ObjectIndex;
下面是加载窗口时的代码

 private void Bookings_Loaded(object sender, RoutedEventArgs e)
    {


       WpfApplication7.AllensCroftEntities1 allensCroftEntities1 = new WpfApplication7.AllensCroftEntities1();
        // Load data into Rooms.
        var roomsViewSource = ((CollectionViewSource)(this.FindResource("roomsViewSource")));
        var roomsQuery = this.GetRoomsQuery(allensCroftEntities1);
        roomsViewSource.Source = roomsQuery.Execute(MergeOption.AppendOnly);

    }
以下是导致错误的“删除”按钮的代码:

 private void btnDelete_Click(object sender, RoutedEventArgs e)
    {

        //prevents user trying to delete nothing/unselected row
        if (ObjectIndex == null)
        {
            MessageBox.Show("Cannot delete the blank entry");
        }
        else
        {

            //deletes object from dataset, saves it and outputs a message
            allensCroftEntities1.DeleteObject(ObjectIndex);

            allensCroftEntities1.SaveChanges();
            MessageBox.Show("Booking Deleted");
        }
    }
以下是对象索引的代码:

   private void listrow_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        //outputs index of the selected room
        ObjectIndex = listrow.SelectedItem as Booking;
    }
编辑

我试过了

 allensCroftEntities1.Attach(ObjectIndex);

编辑

当我注释掉bookings loaded事件中的实体时,我得到了这个错误,这段代码过去工作正常,但我不知道为什么会出现所有这些问题


首先尝试将实体附加到上下文:

allensCroftEntities1.Attach(ObjectIndex);
allensCroftEntities1.DeleteObject(ObjectIndex);
或者通过删除该行,使用类范围中声明的上下文而不是
Bookings\u Loaded
事件处理程序中的本地上下文:

WpfApplication7.AllensCroftEntities1 allensCroftEntities1 = new WpfApplication7.AllensCroftEntities1();

我已经这样做了,请参见主窗口部分下的代码是的,您已经在类范围中声明了上下文,但是在
Bookings\u Loaded
中有另一个同名的局部变量,我尝试过,另一个错误,请参见最新编辑,这过去是有效的,但我不知道为什么id不再有效。最后一个看起来像非EF连接。我将取消对该实体的注释。我已编辑了您的标题。请看“”,其中的共识是“不,他们不应该”。谢谢你的建议