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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:14:14+00:00 2026-05-17T16:14:14+00:00

If i have the following entity: public class PocoWithDates { public string PocoName {

  • 0

If i have the following entity:

public class PocoWithDates
{
   public string PocoName { get; set; }
   public DateTime CreatedOn { get; set; }
   public DateTime LastModified { get; set; }
}

Which corresponds to a SQL Server 2008 table with the same name/attributes…

How can i automatically:

  1. Set the CreatedOn/LastModified field for the record to now (when doing INSERT)
  2. Set the LastModified field for the record to now (when doing UPDATE)

When i say automatically, i mean i want to be able to do this:

poco.Name = "Changing the name";
repository.Save(); 

Not this:

poco.Name = "Changing the name";
poco.LastModified = DateTime.Now;
repository.Save();

Behind the scenes, “something” should automatically update the datetime fields. What is that “something”?

I’m using Entity Framework 4.0 – is there a way that EF can do that automatically for me? (a special setting in the EDMX maybe?)

From the SQL Server side, i can use DefaultValue, but that will only work for INSERT’s (not UPDATE’s).

Similarly, i can set a default value using a constructor on the POCO’s, but again this will only work when instantiating the object.

And of course i could use Triggers, but it’s not ideal.

Because i’m using Entity Framework, i can hook into the SavingChanges event and update the date fields here, but the problem is i need to become “aware” of the POCO’s (at the moment, my repository is implemented with generics). I would need to do some sort of OO trickery (like make my POCO’s implement an interface, and call a method on that). I’m not adversed to that, but if i have to do that, i would rather manually set the fields.

I’m basically looking for a SQL Server 2008 or Entity Framework 4.0 solution. (or a smart .NET way)

Any ideas?

EDIT

Thanks to @marc_s for his answer, but i went with a solution which is better for my scenario.

  • 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-17T16:14:14+00:00Added an answer on May 17, 2026 at 4:14 pm

    As i have a service layer mediating between my controllers (im using ASP.NET MVC), and my repository, i have decided to auto-set the fields here.

    Also, my POCO’s have no relationships/abstractions, they are completely independant. I would like to keep it this way, and not mark any virtual properties, or create base classes.

    So i created an interface, IAutoGenerateDateFields:

    public interface IAutoGenerateDateFields
    {
       DateTime LastModified { get;set; }
       DateTime CreatedOn { get;set; }
    }
    

    For any POCO’s i wish to auto-generate these fields, i implement this inteface.

    Using the example in my question:

    public class PocoWithDates : IAutoGenerateDateFields
    {
       public string PocoName { get; set; }
       public DateTime CreatedOn { get; set; }
       public DateTime LastModified { get; set; }
    }
    

    In my service layer, i now check if the concrete object implements the interface:

    public void Add(SomePoco poco)
    {
       var autoDateFieldsPoco = poco as IAutoGenerateDateFields; // returns null if it's not.
    
       if (autoDateFieldsPoco != null) // if it implements interface
       {
          autoDateFieldsPoco.LastModified = DateTime.Now;
          autoDateFieldsPoco.CreatedOn = DateTime.Now;
       }
    
       // ..go on about other persistence work.
    }
    

    I will probably break that code in the Add out to a helper/extension method later on.

    But i think this is a decent solution for my scenario, as i dont want to use virtuals on the Save (as i’m using Unit of Work, Repository, and Pure POCO’s), and don’t want to use triggers.

    If you have any thoughts/suggestions, let me know.

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

Sidebar

Related Questions

I have the following entity structure: public class Party { public Int32 PartyId {
I have the following entity class (in Groovy): import javax.persistence.Entity import javax.persistence.Id import javax.persistence.GeneratedValue
I have a problem with the following Linq query using Entity Framework: from o
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following situation. A main table and many other tables linked together with
I have following text in a file 23456789 When I tried to replace the

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.