简体   繁体   English

从gridview删除选定的实体对象,外键表将其阻塞

[英]Delete selected entity object from gridview,where foreign key table block it

These are the tables Employee , EmployeeTicket , Ticket in my SQL Server database: 这些是我的SQL Server数据库中的表EmployeeEmployeeTicketTicket

http://i62.tinypic.com/709q50.png http://i62.tinypic.com/709q50.png

And this is how its seen in my Entity Framework model 这就是在我的实体框架模型中看到的样子

http://i57.tinypic.com/30w9fup.png http://i57.tinypic.com/30w9fup.png

As you see my foreign key table becomes a navigation property and it's OK I found a way to use it. 如您所见,我的外键表成为导航属性,没关系,我找到了一种使用它的方法。

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

The problem is I get an error: 问题是我得到一个错误:

The DELETE statement is in conflict with the constraint REFERENCE "FK_EmployeeTicket_Employee". DELETE语句与约束REFERENCE“ FK_EmployeeTicket_Employee”冲突。 The conflict occurred in the table "dbo.EmployeeTicket", column 'ID' database "TrackUser" 在表“ dbo.EmployeeTicket”的列“ ID”数据库“ TrackUser”中发生了冲突

Which means I can't delete ID from Employee and I think I should first delete the ID in foreign key table. 这意味着我不能从Employee删除ID,我认为我应该先删除外键表中的ID。 How can I achieve that? 我该如何实现? Or is there any simple change I can do to get rid of this error? 还是我可以做一些简单的更改来摆脱此错误?

u can try to make EmployeeTicket ID as allows NULL depends on how set the colums properties 您可以尝试使EmployeeTicket ID为NULL取决于如何设置colums属性

for more info take a look at here 欲了解更多信息, 请看这里

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

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