简体   繁体   中英

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

These are the tables Employee , EmployeeTicket , Ticket in my SQL Server database:

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

And this is how its seen in my Entity Framework model

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". The conflict occurred in the table "dbo.EmployeeTicket", column 'ID' database "TrackUser"

Which means I can't delete ID from Employee and I think I should first delete the ID in foreign key table. 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

for more info take a look at here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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