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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:37:56+00:00 2026-06-18T03:37:56+00:00

I am using EF 5.0 and the model first approach. I have build a

  • 0

I am using EF 5.0 and the model first approach. I have build a GenericRepository that has the basic get, insert, delete etc statements. Like:

public virtual void Insert(TEntity entity)
{
      dbSet.Add(entity);
}

My EF entities all have the attributes Modified and ModifiedBy. Now I want to change this values everytime I save an entity.
Is it possible to modify this two attributes (set the value) without writing an specific implementation all the time?

Thank you

  • 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-06-18T03:37:57+00:00Added an answer on June 18, 2026 at 3:37 am

    I see two options for you to do this, but they both entail either introducing a base type or an interface for all of your entities to cover them in a generic function. I would prefer an interface, although each entity would have to implement it again and again.

    Let’s say you create

    interface IAuditable
    {
        DateTime Modified { get; set; }
        string ModifiedBy {get; set; } // User id?
    }
    

    Now you can do:

    public virtual void Insert(TEntity entity)
       where TEntity : IAuditable
    {
         entity.Modified = DateTime.Now;
         entity.ModifiedBy = ???? // Whatever you get the name from
         ...
    }
    

    (Same for edit)

    You can also subscribe to the context’s SavingChanges event:

    // In the constructor:
    context.SavingChanges += this.context_SavingChanges;
    
    private void context_SavingChanges(object sender, EventArgs e)
    {
        foreach (var auditable in context.ObjectStateManager
            .GetObjectStateEntries(EntityState.Added | EntityState.Modified)
            .Select(entry => entry.Entity)
            .OfType<IAuditable>)
        {
            auditable.Modified = DateTime.Now;
            auditable.ModifiedBy = ????;
        }
    }
    

    If you work with DbContext you can get to the event by

    ((IObjectContextAdapter)this).ObjectContext.SavingChanges
    

    I’d like to add that more reliable time tracking can (and maybe should) be achieved by database triggers. Now you depend on a client’s clock.

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

Sidebar

Related Questions

I'm using the Code First approach and have the following Model: public class Person
I'm using the model-first approach offered by the EF4. I have two entities: User
I have a model created using Model First approach. When I try to access
I have a model as shown below (created using Model-First approach). A book is
I'm Using EF 4.1 Model First approach Is it possible to have a foreign
I've been using EF for a while now, always in the Model-First approach. Now
Im using MVC-Viewmodel, EF Model first on my project. I have a foreach loop
I am using MVC3, EF Model first. I have a form with two DropDownList
I am using MVC3, EF Model first on my project. I have a view
I am using MVC3, EF Model first on my project. I have a view

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.