Hello friends, good morning.
I have the following drawback, a many-to-many table.
works perfectly up to here, I hope it is well implemented
I show the image
I enter this data in this way
now i want to remove a rule from a certain profile
in this case remove the Employee profile, Create Client and Edit Client. The idea I have is to delete all the rules assigned to the employee and then again enter the new rules (In this case only one rule)
I did this method
- public static void InsertarPerfilRegla(tbltransPerfilRegla tbltransPerfilRegla)
- {
- using (GourmetEntities db = new GourmetEntities())
- {
-
- db.tbltransPerfilReglas.RemoveRange(db.tbltransPerfilReglas.Where(x => x.Perfil_Id == tbltransPerfilRegla.Perfil_Id));
- db.SaveChanges();
- db.tbltransPerfilReglas.Add(tbltransPerfilRegla);
- db.SaveChanges();
- }
-
- }
When I only run the code block
- db.tbltransPerfilReglas.RemoveRange(db.tbltransPerfilReglas.Where(x => x.Perfil_Id == tbltransPerfilRegla.Perfil_Id));
- db.SaveChanges();
Works correctly
But when I run both codes it shows me an error
This is the code in my Button
- private void TsbGuardar_Click(object sender, EventArgs e)
- {
- tbltransPerfilRegla tbltransPerfilRegla = new tbltransPerfilRegla();
- using (GourmetEntities db = new GourmetEntities())
- {
- tbltransPerfilRegla.Perfil_Id = (int)CboPerfiles.SelectedValue;
- foreach (DataGridViewRow row in DatagridViewPerfilRegla.Rows)
- {
- if ((bool)row.Cells["Selected"].Value)
- {
-
- tbltransPerfilRegla.Regla_Id = (int)row.Cells[1].Value;
- }
- tbltransPerfilRegla.InsertarPerfilRegla(tbltransPerfilRegla);
- }
- }
- }
Please can you help me to solve this problem that I have, or can you tell me another way to do this please.
I thank you very much
Rob