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

  • Home
  • SEARCH
  • 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 3215280
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:09:33+00:00 2026-05-17T15:09:33+00:00

Okay, I’ve been struggling with this for a while now. I have a standard

  • 0

Okay, I’ve been struggling with this for a while now.

I have a standard MVC2 project and use EF4 to access data objects. Many of my data objects (and therefore database tables) have two common fields: UpdateUserAccountID and UpdateDate.

In each of my controllers, I am currently setting this fields manually within each controller on the Edit action:

    // POST: /Modes/Edit/2
    [HttpPost]
    public ActionResult Edit(int id, FormCollection formValues)
    {
        Mode mode = _repository.GetMode(id);

        //Set update flags
        mode.UpdateUserAccountID = _userRepository.GetUserID(User.Identity.Name);
        mode.UpdateDate = DateTime.Now;

        try
        {
             UpdateModel(mode);
             _repository.Save();
             return RedirectToAction("Details", new { id = mode.ModeID });
         }
         catch
         {
             return View(mode);
         }

    }

Problem is, I want to update these two fields in a central place, regardless of the type of the controller. I am using Repository/IRepository pattern for my data objects. I also have set up a BaseController as I thought I could pass in my ‘mode’ object in the example above to a generic function in a base controller to set these two fields. But, I couldn’t figure that out and the generic function has no awareness of these specific fields.

Should I be creating a base class for my repositories instead? I can’t envisage how that would work within the MVC framework. In webforms, I would just have had an interface that all my models implement with the common fields but i can’t seem to fit it in to an MVC approach.

Any help as to the approach to take would be great.

  • 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-17T15:09:34+00:00Added an answer on May 17, 2026 at 3:09 pm

    This post contains a detailed solution for this scenario:
    Entity Framework – Auditing Activity

    Basically, the idea is how to update common properties like LastModifiedBy or LastModifiedDate on all entities before they saved on the data store at the EF data access layer with one common code.

    You should get rid of the 2 lines of codes where you Set update flags and let that all happens in the Context_SavingChanges() like the way it has been described in the above link. Since you are in a web scenario, you have to pass User.Identity.Name back to your Repository.Save() method and then invoke Repository.GetUserID() on the Context_SavingChanges() as well.

    This way, you don’t need to update these 2 properties each and every time you insert or update an entity to the data store. It will automatically all happens in one place just before saving.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.