Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding SaveChanges and setting ModifiedDate, but how do I set ModifiedBy?

Tags:

I have an ASP.NET MVC3 web application with UI, Business (entities), and Data (DbContext) layers. I am using Entity Framework 4.1 Code First. Right now, I am overriding the DbContext.SaveChanges() in the Data layer so that I can set the ModifiedDate for all changes made to any entity objects that implement my IAuditable interface. I have a static DateProvider class and method (GetCurrentDate) that returns DateTime.Now (unless I'm running a test, in which case, it returns whatever I told it to).

I would like to automatically set the ModifiedBy property to the current user as well. What is the best way to go about doing this? Is there something that is built in the framework that will allow me to access this information or do I need to set something up kind of like the DateProvider class? This is an Active Directory environment and we use WindowsAuthentication in IIS.

Here is my SaveChanges code:

public override int SaveChanges() {     var changeSet = ChangeTracker.Entries<IAuditable>();      if (changeSet != null)     {         foreach (var entry in changeSet.Where(c => c.State != EntityState.Unchanged))         {             entry.Entity.ModifiedDate = DateProvider.GetCurrentDate();         }     }     return base.SaveChanges(); } 
like image 892
norepro Avatar asked Oct 03 '11 22:10

norepro


People also ask

How do I save changes in Entity Framework?

Entity Framework Core Save Changes to the database using the SaveChanges method of DbContext. When we use the SaveChanges it prepares the corresponding insert , update , delete queries. It then wraps them in a Transaction and sends them to the database. If any of the queries fails all the statements are rolled back.

What does the DbContext SaveChanges () method return?

Returns. The number of state entries written to the underlying database. This can include state entries for entities and/or relationships.

What is SaveChanges?

SaveChanges()Persists all updates to the data source and resets change tracking in the object context. public: int SaveChanges();


1 Answers

You can use the HttpContext.Current.User.Identity.Name to get the name of the current user.

public override int SaveChanges() {     var changeSet = ChangeTracker.Entries<IAuditable>();      if (changeSet != null)     {         foreach (var entry in changeSet.Where(c => c.State != EntityState.Unchanged))         {             entry.Entity.ModifiedDate = DateProvider.GetCurrentDate();             entry.Entity.ModifiedBy = HttpContext.Current.User.Identity.Name;         }     }     return base.SaveChanges(); } 

Better way to do this would be to use constructor injection to pass the current user to the context

public class MyContext : DbContext {     public MyContext(string userName)     {         UserName = userName;     }      public string UserName     {         get; private set;     }      public override int SaveChanges()     {        var changeSet = ChangeTracker.Entries<IAuditable>();         if (changeSet != null)        {           foreach (var entry in changeSet.Where(c => c.State != EntityState.Unchanged))           {               entry.Entity.ModifiedDate = DateProvider.GetCurrentDate();               entry.Entity.ModifiedBy = UserName;           }        }        return base.SaveChanges();    } } 
like image 70
Eranga Avatar answered Oct 05 '22 19:10

Eranga