Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6641077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:46:05+00:00 2026-05-25T23:46:05+00:00

I have an ASP.NET MVC3 web application with UI, Business (entities), and Data (DbContext)

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:46:05+00:00Added an answer on May 25, 2026 at 11:46 pm

    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();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC 3 Web Application using Linq-to-SQL for my data access
I have created a sample project using ASP.NET MVC 3 Web Application (Razor) template.
I have an ASP.NET MVC 3 (.NET 4) web application. This app fetches data
I'm working on a web application project using ASP.NET MVC3 and database in SQL
I have a web application which was using Asp.net MVC2 . I Upgraded it
I have an ASP.NET MVC 3 / .NET Web Application, which is heavily data-driven,
I have a web application written in ASP .NET MVC 3. I'm using ACS
We have a web application written in ASP.NET for .NET 3.5, using standard web
I have an ASP.NET MVC3 web application with C# and Razor. Through a form
I have Ninject working nicely in an ASP.NET MVC3 web application. I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.