Delete record in table with relation much to much EF 6

0

I am trying to delete a record from an intermediate table, that is, from a table with a many-to-many relation. I would like to know how I can delete a record from that table because when I try to save the record I do not get an error, but the record is not deleted. The structure that I have is the following:

Tables

  • PROFILE (ID_PERFIL, DESCRIPTION, STATUS)
  • APPLICATIONS (ID_APLICATION, DESCRIPTION, STATUS)
  • PROFILE_APPLICATIONS (ID_APPLICATION, PRO_ID)

Before using entity, what I did was that before inserting into the intermediate table, I eliminated everything in the PROFILE_APPLICATIONS table and then reinserted it with the new records that I have (I have that record in a datagridview, where the user can add and delete and finally save in the database).

I would like to know if there is a way to delete the intermediate table without having to go through the entity, that is, without doing a foreach.

This is my code:

if (dgvAplicacion.Rows.Count > 0)
{
    foreach (DataGridViewRow row in dgvAplicacion.Rows)
    {
        var idapl = row.Cells["ID_APLICACION"].Value.ToString();
        perfil.AM_PERFIL_TBL.Find(id).AM_APLICACIONES_TBL.Add(perfil.AM_APLICACIONES_TBL.Find(idapl));
    }
}
    
asked by Luis Paulino 01.07.2017 в 02:59
source

1 answer

0

Well I do not know if it's the best option I've used, but I did it as I did before using entity, first I delete the intermediate table and then insert the records as follows:

if (dgvAplicacion.Rows.Count > 0)
{
   //esta es la linea que he agregado para eliminar los registro
   perfil.AM_PERFIL_TBL.Find(id).AM_APLICACIONES_TBL.Clear();
    foreach (DataGridViewRow row in dgvAplicacion.Rows)
    {
        var idapl = row.Cells["ID_APLICACION"].Value.ToString();
        perfil.AM_PERFIL_TBL.Find(id).AM_APLICACIONES_TBL.Add(perfil.AM_APLICACIONES_TBL.Find(idapl));
    }
}

In case you have a better and more feasible way I will be grateful

    
answered by 01.07.2017 в 03:50