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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:46:11+00:00 2026-05-12T06:46:11+00:00

Say I want to create a class called EntityWithCreatedAt: class Entity<T> : ActiveRecordBase<T> {

  • 0

Say I want to create a class called EntityWithCreatedAt:

class Entity<T> : ActiveRecordBase<T>
{
  [PrimaryKey]
  public int Id { get; set; }
}

class EntityWithCreatedAt<T> : Entity<T>
{
  [Property]
  public DateTime CreatedAt { get; set; }
}

What is the best way to populate the CreatedAt field? Do I just do it in EntityWithCreatedAt’s constructors, or is there some other way?

What about an UpdatedAt property?

Thanks,

  • 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-12T06:46:11+00:00Added an answer on May 12, 2026 at 6:46 am

    In Rails’ implementation of AR, CreatedAt and UpdatedAt are automatic timestamps created in migrations (although you can created them manually, too). Assuming you want the same behaviour, you’ll need to override Create and Update.

       public override void Update() 
       {
             UpdatedAt = DateTime.Now;                      
             base.Update(); 
       }
    
       public override void Create()
       {
             CreatedAt = DateTime.Now;
             base.Create();
       }
    

    If you’re not using an assigned primary key, and the DB is generating it for you (with auto increment, for example), then you may be using the Save method to decide whether to call Create or Update. Using Save will still work perfectly for you, as a call to the base class’ Save() method will trigger a call to your Create (if the ID hasn’t been set) or your Update (if the ID has been set and the record has previously been saved).

    The only downside to this method is that your CreatedAt and UpdatedAt properties should always reflect what’s saved in the DB, but in your case you’re setting the properties before you know the commit to the DB succeeded. This is unavoidable, but with some try/catch code in your Update override, you should be able to record the previous value and assign it back should anything go wrong:

       public override void Update() 
       {
             DateTime originalUpdatedAt = UpdatedAt;
             try 
             {
                UpdatedAt = DateTime.UtcNow;  // Assuming you're using UTC timestamps (which I'd recommend)                     
                base.Update(); 
             }
             catch  // Don't worry, you're rethrowing so nothing will get swallowed here.
             {
                UpdatedAt = originalUpdatedAt;
                throw;
             }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 159k
  • Answers 159k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Which servlet container are you using? The docs for getHeader(String)… May 12, 2026 at 11:22 am
  • Editorial Team
    Editorial Team added an answer Can't you have 2px left-margin instead of 1px on each… May 12, 2026 at 11:22 am
  • Editorial Team
    Editorial Team added an answer Look at the parameters xaxs and yaxs (under ?par). By… May 12, 2026 at 11:22 am

Related Questions

I mainly develop in native C++ on Windows using Visual Studio. A lot of
I am trying to bind an event to a method of a particular instance
I have a deceptively simple scenario, and I want a simple solution, but it's
UPDATED I've updated the example to better illustrate my problem. I realised it was
Say I have a class named Frog, it looks like: public class Frog {

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.