Search code examples
asp.net-mvclinqasp.net-mvc-4lambdahttp-post

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


I am doing a project and in that I two table agent and user. The user table contains a foreign key. so here I need to delete an item in the 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 other that was deleted.


Solution

  • this error occurs when your table is dependent on another table in your case its one to many relationship so add this in your dbcontext

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AgentMaster>()
        .HasOptional(c => c.Users)
        .WithOptionalDependent()
        .WillCascadeOnDelete(true);
    }