EntityFramework not updating column with default value

Just apply the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute to on the column field in your entity object definition.

For example:

public class SomeTable
{
    ...

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public DateTime CreatedDate { get; set; }

    ...
}

This tells the Entity Framework that your column's initial value is supplied by the database. The value will update automatically from the database after row insertion.


If you never want to edit that value (like with a created date), you can use:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public virtual DateTime CreatedDate { get; set; }

This will tell the Entity Framework that the value is controlled by the database, but will still fetch the value.

Note that you then cannot change that value, so it's not a solution if you simply want an initial value.

If you just want a default value but are still allowed to edit it, or you are using the Entity Framework 5 and below, you have to set the default in code.

More discussion about this here:

How to use Default column value from DataBase in Entity Framework?