0

I have code like this:

[HttpPost]
public static bool AggiornaInstallatore(FormCollection form)
{
    using (var ctx = new termostore_installazione_Entities())
    {
        var idr = Convert.ToInt32(form["id_installatore"]);
        var installatore = ctx.installatori.Where(x => x.Id == idr).FirstOrDefault();
        var squad = Convert.ToInt16(form["squadra"]);
        var squadra = ctx.squadra.Where(x => x.id == squad).FirstOrDefault();

        if (form["edit_nome"] != "") { installatore.nome = form["edit_nome"]; }
        if (form["edit_cognome"] != "") { installatore.cognome = form["edit_cognome"]; }
        if (form["edit_email"] != "") { installatore.mail = form["edit_email"]; }
        if (form["edit_telefono"] != "") { installatore.telefono = form["edit_telefono"]; }
        if (form["edit_indirizzo"] != "") { installatore.indirizzo = form["edit_indirizzo"]; }
        if (form["edit_zona_competenza"] != "") { installatore.zona_competenza = form["edit_zona_competenza"]; }

        installatore.squadra.Add(squadra);

        ctx.SaveChanges();

    }
    return true;
}

Now, instead of adding squadra I want to update this record, but I don't know how to do it.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    Just do the changes that you want to do in "squadra". The changes will be persisted through ctx.SaveChanges(). – javier_el_bene Jun 26 '17 at 14:22
  • Simply call `SaveChanges`. Entities are tracked in your context, when changes are made then the item will be marked as state `Modified` – ColinM Jun 26 '17 at 14:22
  • 3
    Don't do database operations directly in your controller! Follow some separation of concerns. Have a separate database layer responsible for that. – mason Jun 26 '17 at 14:25
  • Possible duplicate of [How to update record using Entity Framework 6?](https://stackoverflow.com/questions/25894587/how-to-update-record-using-entity-framework-6) – Craig W. Jun 26 '17 at 15:58
  • The junction table record represents a simple link, what really do you want to "update" of that record? – Ivan Stoev Jun 26 '17 at 16:13

1 Answers1

1

Instead of add the element squadra to the list, you can update every single element of the object from the db like that:

//The element you already have in your example
var squadra = ctx.squadra.Where(x => x.id == squad).FirstOrDefault();

squadra.name = form.name; //I assume you get te updated value from form
squadra.type = form.type;
...

ctx.SaveChanges();

You have the squad element updated and, with the SaveChanges method, the updates are set even on the db.

Dr. Roggia
  • 1,095
  • 3
  • 16
  • 40
  • But the situation is more complex... *installatore.squadra* is a junction table between two tables which have many to many relationship. Then, I will reply my question: how can I update the record? – Salvatore Riccardi Jun 26 '17 at 14:55
  • Is it a view then? – Dr. Roggia Jun 26 '17 at 15:02
  • There are three tables: first table *installatori*, second table *squadra* and third junction table *appartiene_squadra* which is beetween the first two tables. I want update record of the junction table. – Salvatore Riccardi Jun 26 '17 at 16:07