Often times in my SQL Server database, I’ll declare a CreatedDate field. It is set to non-nullable and the default value is GetDate(). This works just fine to force a created date.
I can’t fgure out how to do this using EF4.1. I see in the designer there is a place for Default Value, but I’m not sure if I can enter a function for this.
I also tried to set it as nullable in the EF designer, and just let SQL Server do the populating, but it didn’t like this.
Any advice?
You cannot use
GetDatewhen using EF unless you markCreatedDatecolumn asStoreGeneratedPatterntoIdentity. In such case you will not be able to set this value in your application and database will be responsible for that.If you don’t set
StoreGeneratedPatternyou must set the data in your application (designer doesn’t allow setting default value to current date). So you must either manually populateCreatedDateor use some trick like your own partial part of the entity class with default constructor setting this property: