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.

313 votes
Vote 0 votes Vote Vote
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service

    You'll receive a confirmation email with a link to create a password (optional).

    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Mike FlaskoAdminMike Flasko (Lead Program Manager, DataFx) shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →
    StrandedPirateStrandedPirate shared a merged idea: Provider specific default constraints  ·   ·  Show description
    BLahrBLahr shared a merged idea: Add default value constraint when using CreateDatabase  ·   ·  Show description

    8 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service

      You'll receive a confirmation email with a link to create a password (optional).

      Signed in as (Sign out)
      Submitting...
      • 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.

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

      Knowledge Base and Helpdesk