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 740145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:28:54+00:00 2026-05-14T08:28:54+00:00

I have an entity model that has audit information on every table (50+ tables)

  • 0

I have an entity model that has audit information on every table (50+ tables)

 CreateDate
 CreateUser
 UpdateDate
 UpdateUser

Currently we are programatically updating audit information.

Ex:

  if(changed){
        entity.UpdatedOn = DateTime.Now;
        entity.UpdatedBy = Environment.UserName;
        context.SaveChanges();
   }

But I am looking for a more automated solution. During save changes, if an entity is created/updated I would like to automatically update these fields before sending them to the database for storage.

Any suggestion on how i could do this? I would prefer to not do any reflection, so using a text template is not out of the question.

A solution has been proposed to override SaveChanges and do it there, but in order to achieve this i would either have to use reflection (in which I don’t want to do ) or derive a base class. Assuming i go down this route how would I achieve this?

For example

EXAMPLE_DB_TABLE
 CODE
 NAME
 --Audit Tables
 CREATE_DATE
 CREATE_USER
 UPDATE_DATE
 UPDATE_USER

And if i create a base class

 public abstract class IUpdatable{

     public virtual DateTime CreateDate {set;}
     public virtual string CreateUser { set;}
     public virtual DateTime UpdateDate { set;}
     public virtual string UpdateUser { set;}
 }

The end goal is to be able to do something like…

   public overrride void SaveChanges(){
        //Go through state manager and update audit infromation
        //FOREACH changed entity in state manager
        if(entity is IUpdatable){
           //If state is created... update create audit.
           //if state is updated... update update audit
        }
   }

But I am not sure how I go about generating the code that would extend the interface.

  • 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-14T08:28:54+00:00Added an answer on May 14, 2026 at 8:28 am

    Here was the final solution.

    I basically check my model for the presence of 4 properties. If the entity contains

    InsertedOn and InsertedBy and UpdatedOn and UpdatedBy I generate (using a tt file) the following entity that implements an IAuditable interface.

    public interface IAuditable
    {
        void SetInsertedOn(DateTime date);
        void SetInsertedBy(string user);
        void SetUpdatedOn(DateTime date);
        void SetUpdatedBy(string user);
    }
    
    public partial class ExampleDBtable: EntityObject, Audit.IAuditable
    {
         //Normal EDMX stuff here..
         public static ExampleDBtable CreateExampleDBtable(int id, string code, string name, DateTime insertedOn, string insertedBy, DateTime updatedOn, string updatedBy)
           {
    
           }
           //Extension points.
        #region IAuditable Members
    
        public void SetInsertedOn(DateTime date)
        {
            this._InsertedOn = date;
        }
    
        public void SetInsertedBy(string user)
        {
            this._InsertedBy = user;
        }
    
        public void SetUpdatedOn(DateTime date)
        {
            this._UpdatedOn = date;
        }
    
        public void SetUpdatedBy(string user)
        {
            this._UpdatedBy = user;
        }
    
        #endregion
    
        }
    

    I also generate code to handle the updating of audit information

       public ExampleDBObjectContext(string connectionString) : base(connectionString, "publicExampleDBObjectContext")
        {
            this.SavingChanges += new EventHandler(UpdateAuditInformation);
            this.OnContextCreated();
         }
    

    And then finally I implemented the UpdateAuditInformation

       foreach (ObjectStateEntry entry in
                context.ObjectStateManager.GetObjectStateEntries(
                EntityState.Added | EntityState.Modified))
            {
                  //Start pseudo code..
                  if(entry.Entity is IAuditable){
                     IAuditable temp = entry.Entity as IAuditable;
                     temp.SetInsertedOn(DateTime.Now);
                     temp.SetInsertedBy(Env.GetUser());
                     ...
    
                  }
            }
    

    I opted not to show the .tt file because it looks horrible in the editor.

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

Sidebar

Related Questions

In my business model I have Entity class (IPoint interface ) that has a
In my core data model I have a Person entity that has a to
I have two tables that are considered a single entity in my domain model.
I have a Core Data model entity NoteObject that has a transformable type arrayOfTags
I have an object defined by my entity framework model that has navigation properties,
I'm using the Play! framework and I have a model (an Entity) that has
I have an entity model which has some tables added from my DB. I
I have a dll that has an entity model that does a particular job.
I have an Entity which is basically one Datamodel. That Model has several Columns
I have an EntityFramework model that has User entity that has an EntityCollection of

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.