[英]Delete selected entity object from gridview,where foreign key table block it
这些是我的SQL Server数据库中的表Employee
, EmployeeTicket
, Ticket
:
http://i62.tinypic.com/709q50.png
这就是在我的实体框架模型中看到的样子
http://i57.tinypic.com/30w9fup.png
如您所见,我的外键表成为导航属性,没关系,我找到了一种使用它的方法。
// Use a LINQ expression to find the selected product.
int selectedID = (int)GridViewTicketHistory.SelectedDataKey.Value;
var matches = from p in entities.Employees
where p.ID == selectedID
select p;
// Execute the query and return the entity object.
Employee emp = matches.Single();
// Delete the entity object.
entities.Employees.DeleteObject(emp);
// Commit the changes back to the database.
entities.SaveChanges();
editCustomerTicket();
问题是我得到一个错误:
DELETE语句与约束REFERENCE“ FK_EmployeeTicket_Employee”冲突。 在表“ dbo.EmployeeTicket”的列“ ID”数据库“ TrackUser”中发生了冲突
这意味着我不能从Employee
删除ID,我认为我应该先删除外键表中的ID。 我该如何实现? 还是我可以做一些简单的更改来摆脱此错误?
您可以尝试使EmployeeTicket ID为NULL取决于如何设置colums属性
欲了解更多信息, 请看这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.