Search for existing suggestions

Allow default date = NOW and provider.MinDate in CSDL

get asked about non-nullable date defaults frequently.

The only way to set a default date in the entity data model is to put a hard date in there, a string. If you have a known default date (e.g. 1900-01-01) this is fine. But often we want NOW to be the default and there's now way to indicate that in the model. Additionally, if there was a way to indicate EF should use the providers minimum value that would be nice. Even if it meant typing "SqlServer.MinDate' into the default attribute in the property window.

Even if the database knows how to set the default value, we can't benefit from that and leave the date empty when updating (value type such thing as empty). We can't make it computed or it essentially makes the field read only.

I personally use validation code or constructors to set a default date, but I think that many people would benefit by being able to ahve a way to set "now" as a default directly in the model.

289 votes
Sign in
Sign in with: facebook google
Signed in as (Sign out)

We’ll send you updates on this idea

Mike Flasko shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

Marking as completed based on the fact that the general scenario is support in EF Core via SQL fragments as default values. EF6 also supports specifying those in migrations.

We are not planning to implement the exact feature request in either EF6.


Sign in
Sign in with: facebook google
Signed in as (Sign out)
  • Nigel Rheam commented  ·   ·  Flag as inappropriate

    +1 vote, though in my use-case, I am looking for a Data Annotation for Code First that allows us to specify a default column value - even if only for adding new not-null columns. (There is a similar item 'Support database default values in Code First' which I have also voted on)

  • Anonymous commented  ·   ·  Flag as inappropriate

    we can set the default date to null on model creating by changing the column type to datetime2

  • Anonymous commented  ·   ·  Flag as inappropriate

    Any update on that? I am using the Design First approach (starting to regret that now...) for a quite large web project and this feature would massively simplify things. Honestly, EF is a brilliant tool, but I simply can't believe that such basic things, like default date=NOW are missing.
    Also if that gets added, would a "ON UPDATE NOW" option be possible as well?

  • Mike Bishop commented  ·   ·  Flag as inappropriate

    Totally agree. Would prefer this be done with Fluent API via the .Property (p => p.YourProperty). This could be used for a host of table column options .. .HasDefault( your default), .IsOptional(Sparse) for sparse columns, etc.

  • Hobbes commented  ·   ·  Flag as inappropriate

    Even better would be the option to default to either MinDate OR MaxDate.

  • HiredMind commented  ·   ·  Flag as inappropriate

    I use "(GETUTCDATE())" as my default - works for me. Of course I'm using Migrator.NET to generate the DB, then using Database-First to generate the Entity Model. Would the designer choke on that?

  • Isaac commented  ·   ·  Flag as inappropriate

    I completely agree. I was shocked to find this out after I imported 50 tables then had to manually craft the defaulting.

    Also, this item should be combined with a few others. I searched for default and found some overlaps.

  • Anthony Johnston commented  ·   ·  Flag as inappropriate

    for POCO this could be a new property on the ColumnAttribute
    [Column("Name", DefaultValue = "(new name)"]

  • Yossu commented  ·   ·  Flag as inappropriate

    Very annoying that you set default values in the database, then the EF wizard ignores them when generating your model. You end up with exceptions when doing an insert or update and you don't explicitly set a value for non-nullable fields that have a default.

    Hey, that's the whole point of a default value! The EF model should pick up the default value and use that if none is supplied.

Feedback and Knowledge Base