Recently at work, I’ve had the pleasure of working on a new project written in ASP.NET MVC2 under .NET 4.0. Our team has decided to use Entity Framework 4 for data access, and for the most part, we’ve been very pleased with this decision. One of the things I tend to make use of pretty regularly is default values for columns in SQL server. In doing this, I’ve encountered a pretty nasty bug in EF4. Here’s the deal:
I have several tables in my application where I add some basic auditing columns, such as CreateDate, CreatedBy, LastUpdatedDate, and LastUpdatedBy. These columns are pretty self explanatory, and I prefer to use the SQL GETDATE() function as the default value for the date columns. This way I don’t have to worry about setting these dates when a new row is inserted, because SQL will handle it for me.
The first thing you have to do is tell EF4 that these columns are computed by SQL server. The way you do this is by opening the Entity Data Model (EDM) and finding the matching columns in each entity. Once you select a column, check out the Properties pane for the column. You should see something like this: