athira k

athira k

  • NA
  • 140
  • 53.7k

How to delete rows of two different table having same id mvc

Apr 23 2017 8:12 AM
I am doing a project and in that I two table agent and user. The user table contains a foreign key called agentid which takes the value from the id of the agent table. so here I need to delete an agent in the agent table and at the same time it must effect to the user table and must delete the row in the user table that having the same id of the agent that was deleted. eg, suppose if i deleted agent 10 , all the details in the id 10 must be deleted along with that the agentid with 10 in the user table must also be deleted.  when I simply delete the agent in agent table, it generates an error that  
The DELETE statement conflicted with the REFERENCE constraint
I dont know how to solve my problem and my code is  
 
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(int id)
{
AgentMaster agentMaster = await db.AgentMasters.FindAsync(id);
db.AgentMasters.Remove(agentMaster);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
 
 This is my controller part, when I tried with join the table and delete the record it always shows the same error . Can anyone please help me to find a solution for my problem ????? 

Answers (2)