我是实体框架的新手。现在,我正在用一个按钮编写删除功能。当我点击此按钮时,我的数据将被删除;但是,它会弹出“对象引用未设置为对象实例”的警报。我想不通这个问题。
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
try
{
if (UserDataGrid.SelectedItems.Count == 0)
return;
if (UserDataGrid.SelectedItems.Count > 0)
if (MessageBox.Show("Would you want to delete?", "Yes", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
var selected = UserDataGrid.SelectedItem as User;
var index = selected.Index;
User user = new User();
user = ssEntities.User.Where(x => x.Index == index).First();
if (user != null)
{
ssEntities.User.DeleteObject(user);
ssEntities.SaveChanges();
}
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally { }
}
如果没有足够的信息,请告诉我添加它。