gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 23:15:03 27 4
gpt4 key购买 nike

我在屏幕截图中发现了问题: problem

这是程序开头的代码:

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

attach

编辑,

当我注释掉预订加载事件中的实体时,出现此错误,此代码过去工作正常,但我不知道为什么会出现所有这些问题。

nullable

最佳答案

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

allensCroftEntities1.Attach(ObjectIndex);
allensCroftEntities1.DeleteObject(ObjectIndex);

或者通过删除该行,使用在类范围内声明的上下文而不是 Bookings_Loaded 事件处理程序中的本地上下文:

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

关于c# - 无法删除该对象,因为在 ObjectStateManager #2 中找不到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15467087/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com