Ignore certain columns on update

asp.net-mvc c# ef-database-first entity-framework

Question

Hello I have something like this:

public ActionResult Edit(int id)
{
    var movie = (from m in _db.Movies where m.Id == id select m).First();

    return View(movie);
}

[HttpPost]
public ActionResult Edit(Movie movie)
{
    try
    {
        var originalMovie = (from m in _db.Movies where m.Id == movie.Id select m).First();

        _db.Movies.ApplyCurrentValues(movie);

        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

This example was taken from Proper way to Edit an entity in MVC 3 with the Entity Framework using Data Model First approach?

I want to pass to the DB SQL-query (UPDATE Movie ....) only modified columns because I'm doing a column audit.

The code works ok, but the problem is that in my "Movie" Entity I have a "FlagOldMovie" property and others 10 properties witch I'm not using its in this view because they will stay the same, but the entityframework put to that properties defaults values so the "ApplyCurrentValues" find changes and that properties are updated too.

A workaround is to pass my not changed properties to html hidden inputs, but its privated data.

Any idea?

1
1
5/23/2017 12:32:56 PM

Accepted Answer

I finally got it, first at all, the solution only works on .NET 4.5+

[HttpPost]
public ActionResult Edit(Movie movie)
{
    try
    {
        //Get DB version
        var originalMovie = (from m in _db.Movies where m.Id == movie.Id select m).First();
        //Mark changes with data received
        _db.Movies.ApplyCurrentValues(movie);

        //CODE ADDED - Ignoring field/properties you dont want to update to DB
        ObjectStateEntry entryToUpdate = db.ObjectStateManager.GetObjectStateEntry(originalMovil);
        entryToUpdate.RejectPropertyChanges("field1");
        entryToUpdate.RejectPropertyChanges("field2");
        entryToUpdate.RejectPropertyChanges("field3");
        //-----------------

        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

With this code, the only data modified is witch you want, next what I did is to audit columns changed extending _db.SaveChanges() to _db.SaveChangesAudito(id);

0
9/2/2013 6:15:24 PM

Popular Answer

[HttpPost]
public ActionResult Edit([Bind(Exclude ="column_name")] Movie movie)
{
//code here
}

This would ignore the column you specify, I usually do that to exclude fields like Id.

But if you are ignoring to many columns then you should consider ViewModel concept where you just have only properties you need for a view.

EDIT: Still some issues?

Here is how you add multiple ones

[HttpPost]
public ActionResult Edit([Bind(Exclude ="c_name, c_name2, c_name3")] Movie movie)
{
//code here
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow