簡體   English   中英

從gridview刪除選定的實體對象,外鍵表將其阻塞

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

這些是我的SQL Server數據庫中的表EmployeeEmployeeTicketTicket

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM