Entity Framework Feature 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 ...no 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.

463 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Mike FlaskoMike Flasko shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →
    StrandedPirateStrandedPirate shared a merged idea: Provider specific default constraints  ·   · 
    BLahrBLahr shared a merged idea: Add default value constraint when using CreateDatabase  ·   · 
    Arda CetinkayaArda Cetinkaya shared a merged idea: Allow setting default values from model  ·   · 

    13 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Nigel RheamNigel 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?
        Cheers

      • Mike BishopMike 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.

      • HobbesHobbes commented  ·   ·  Flag as inappropriate

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

      • HiredMindHiredMind 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?

      • IsaacIsaac 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.

      • YossuYossu 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