Entity Framework 6 Code First default datetime value on insert

Try this:

[DatabaseGenerated(DatabaseGeneratedOption.Identity), DataMember]
public DateTime?            Registered          { get; private set; }

The question mark makes the property nullable


There is no default DateTime. Only a min value.

You could potentially just set the datetime on that entity before calling dbContext.SaveChanges.

See this for a solution.


This technique behaves like a readonly field that is persisted to the database. Once the value is set it cannot (easily) be changed by using code. (Of course, you change the setter to public or internal if needed.)

When you create a new instance of a class that uses this code Registered will not initially be set. The first time the value of Registered is requested it will see that it has not been assigned one and then default to DateTime.Now. You can change this to DateTime.UTCNow if needed.

When fetching one or more entities from the database, Entity Framework will set the value of Registered, including private setters like this.

private DateTime? registered;
[Required]
public DateTime Registered
{
    get
    {
        if (registered == null)
        {
            registered = DateTime.Now;
        }
        return registered.Value;
    }
    private set { registered = value; }
}