Reputation: 6791
I am using EF 6 in code first to make my database and can easily save new and changed data but what I would like to figure out is an easy way to figure out what data was changed when the user edits a page. This is the code I am using in the controller for my edit page:
[Route("Edit"), HttpPost, ValidateAntiForgeryToken]
public ActionResult EditSection([Bind(Include = "ID, RouteName, Type, Title, Synopsis")] Section section, HttpPostedFileBase Logo)
{
SectionAddEditVM model = new SectionAddEditVM { Section = section };
if (ModelState.IsValid)
{
db.Entry(section).State = EntityState.Modified;
db.SaveChanges();
}
return View(model);
}
The only way I could think of is pulling the data from the DB and doing a line by line comparison but that seems overly complicated.
Upvotes: 0
Views: 98
Reputation: 1014
You can further query the Entry for it's changes. Take a look at this SO answer for an example of how to make EF do the work for you. With LINQ, you can easily consume the changes and compose them for human use, auditing, et al.
Upvotes: 0
Reputation: 815
i Think you should use reflection like in this sample
Loop Through An Objects Properties In C#
And compare the two objects in the same way :)
The only thing you should change is the result instead of returning a boolean you could return a list of diffObjects ( just a name for a class that describes the differences.)
Upvotes: 1