How to do Update in Linq?

1

My project has the Delete method, but it definitely deletes the record from the table in the database.

I would like to instead of delete, change the column record, I have a field called "Status", this field is an Enum with two options "Enabled = 0" and "Off = 1", by default it is "Enabled" .

How to change his method for it to change rather than delete?

In MySql I would use this command to change:

update pessoas set Status = 1 where (Id = 1);

On Controller Delete is like this:

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
    {
        Pessoas pessoas = db.Pessoas.Find(id);
        db.Pessoas.Remove(pessoas);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    
asked by anonymous 09.11.2018 / 16:34

3 answers

1

Assuming it has two attributes, name and age and you want to update them:

[HttpPost, ActionName("Edit")]
[ValidateAntiForgeryToken]
public ActionResult EditConfirmed(int id)
{
    Pessoas pessoas = db.Pessoas.Find(id);
    pessoa.Nome = 'Teste';
    pessoa.Idade = 15;
    db.SaveChanges();
    return RedirectToAction("Index");
}

If you do not know how to do this,     

09.11.2018 / 16:41
1

You can do it this way:

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
    Pessoas pessoas = db.Pessoas.Find(id);
    pessoas.Status = Status.Desativado;
    db.Entry(pessoas).State = EntityState.Modified;
    db.SaveChanges();
    return RedirectToAction("Index");
}

First he selects the person, then changes the Status, finally "informs" that his status has been modified and saved.

    
09.11.2018 / 20:03
-1

What you can do is to change your Entities and then use SubmitChanges (), which will persist all changes made until then.

    
09.11.2018 / 16:35