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?