How to Update to Linq?

Asked

Viewed 458 times

2

My project has the Delete method, only it definitely deletes the table record 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 "Disabled = 1", by default is "Enabled".

How to change his method to change instead of delete?

In Mysql I would use this command to change:

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

In Controller Delete looks 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");
    }
  • 2

    LINQ stands for Language Integrated Query, so it makes no sense to use it to make an update, since query is consultation.

  • But the controller has Create and Edit. What would be the correct name of what I am wanting to do?

  • Any other sewing except LINQ.

3 answers

2


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");
}

Just assign the values after having done Find, and have saved, he will understand that it is to do an update

  • Set command does not accept

  • 1

    Try it this way, I updated the answer

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 it selects the person, then it changes the Status, then it "informs" that their state has been modified and saved.

-1

What you can do is change your Entities normally and then use Submitchanges(), which will persist all changes made up to that point.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.