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.
The ability to set the default value on an entity property that is provider specific. E.g. for all of my Date columns I want to set the default value to GETUTCDATE()
When I'm using CreateDatabase() the default values I added in the EF model should also appear in the Database.
we can set the default date to null on model creating by changing the column type to datetime2
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?
AdminDiego Vega (Admin, DataFx) commented
EF added some support for database defaults in Code First Migrations (see http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx). I will keep this idea here to track extending the support to property configuration in Code First and the Entity Designer.
Mike Bishop commented
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.
Even better would be the option to default to either MinDate OR MaxDate.
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?
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
for POCO this could be a new property on the ColumnAttribute
[Column("Name", DefaultValue = "(new name)"]
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.
Ian Mercer commented
I trust you mean UtcNow!