简体   繁体   English

无法从表格中删除行

[英]Cannot delete row from table

I'm trying to delete a row from the table, 我正在尝试从表格中删除一行,

My code : 我的代码:

var strUsername = GridMain.SelectedRows[0].Cells[0].Value.ToString().Trim();
Users selectedUser = _db.Users.First(usr => usr.UserName == strUsername);

_db.DeleteObject(selectedUser);
_db.SaveChanges();

LoadGrid(PresentationMode.Users);

My code is working properly 我的代码正常工作

But for only one row of database, an error occurs on SaveChanges() 但是对于仅一行数据库, SaveChanges()会发生错误

Error Message : 错误信息 :

An error occurred while updating the entries. 更新条目时发生错误。 See the InnerException for details 有关详细信息,请参见InnerException。

Row which the error occurs on that : 发生错误的行:

在此处输入图片说明

Inner Exception: 内部异常:

The DELETE statement conflicted with the REFERENCE constraint "FK_UserReport_Users". DELETE语句与REFERENCE约束“ FK_UserReport_Users”冲突。 The conflict occurred in database "Reporter", table "dbo.UsersReports", column 'UserName'. 数据库“ Reporter”的表“ dbo.UsersReports”的“ UserName”列中发生了冲突。

This Row is available/referenced in UserReport table. 该行在UserReport表中可用/被引用。 So you can't delete it unless you have enabled cascaded delete option. 因此,除非您启用了级联删除选项,否则您将无法删除它。 or else delete the row in UserReport and try delete again. 否则请删除UserReport中的行,然后尝试再次删除。

用户报表中存在引用。因此您无法删除父表值。.首先,删除父子关系或使用级联删除选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM