문제

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?

도움이 되었습니까?

해결책 4

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

다른 팁

[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
}

You can tell EF which fields you want to update. Try something like this:

_db.Movies.Attach(movie);
ObjectStateEntry entryToUpdate = db.ObjectStateManager.GetObjectStateEntry(movie);
entryToUpdate.SetModifiedProperty("field1"); // Replace "field1" with the name of the 1st field to update
entryToUpdate.SetModifiedProperty("field2"); // Replace "field2" with the name of the 2nd field to update
entryToUpdate.SetModifiedProperty("field3"); // Replace "field3" with the name of the 3rd field to update
_db.SaveChanges();

Best practice is to use a ViewModel and not a domain/data model to pass to/from your views. :)

This scenario illustrates one of the dangers of not doing so.

try like that

var originalMovie = (from m in _db.Movies where m.Id == movie.Id select m).First();

originalMovie.updateme = updating;

_db.SaveChanges();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top