I like
http://www.codeproject.com/Articles/34491/Implementing-Audit-Trail-using-Entity-Framework-Pa
but it is not for MVC4.
What easy "ASP.net MVC4 History Log"
solution do you have so the below works ?
Steps
1. user clicks SAVE on Edit.cshtml as normal
2. below code runs as normal
3. somehow (via code or MSSQL database trigger) it writes
only changed values to my "AUDIT_LOG" database table
--------------------------
----------
----------
----------
---------
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(MAINT_WORK_REQ maint_work_req)
{
if (ModelState.IsValid)
{
db.Entry(maint_work_req).S
tate = EntityState.Modified;
try
{
//OurTechSignature_Date
if (maint_work_req.OurTechSig
nature_Dat
e == null)
{
if (Request["Signature_Tech_C
hecked"].C
ontains("t
rue"))
{
var context = new UsersContext();
UserProfile myProfile = context.UserProfiles.Singl
eOrDefault
(u => u.UserName == User.Identity.Name);
maint_work_req.OurTechSign
ature_Date
= myProfile.FirstName + " " + myProfile.LastName + " " + DateTime.Now.ToShortDateSt
ring();
}
}
var context2 = new UsersContext();
UserProfile myProfile2 = context2.UserProfiles.Sing
leOrDefaul
t(u => u.UserName == User.Identity.Name);
maint_work_req.LastModifie
dDate = DateTime.Now;
maint_work_req.LastModifie
dBy = myProfile2.FirstName + " " + myProfile2.LastName;
db.SaveChanges();
.... something like "db.SaveAuditLog()" here
There is not special solution for Audit logs in MVC. Solutions that work anywhere else will work with MVC and Entity Framework.
1. If you use a trigger then that gets executed at the database level and no need to worry about it in your c# code.
2. If you are manually saving to an audit table then simple insert into that table. Wrapping this in a transaction would be handy.
DaTribe