Entity Framework “Code First + Computed GETDATE()”

CFNeed to setup a “Created” DateTime field in your SQL table that is auto-computed to add the current date and time that the field is inserted into the database? 

If you’re doing so in SQL – in the “Default Value or Binding” you just need to enter GETDATE() or GETUTCDATE() – insert a record, and you’ll see the date/time value automatically added to that column.  “…that was easy…”

But how to do the same thing when using Entity Framework Code First?

No big deal here either – as long as you add the couple lines of code described here…

First, we need to add the “DatabaseGenerated” DataAnnotation that describes the “Created” field as a computed field.  See Code First DataAnnotation’s for more…
 

   1:  public class DemoRecord
   2:  {
   3:      public long Id { get; set; }
   4:      public string Name { get; set; }
   5:   
   6:      [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
   7:      public DateTime? Created { get; set; }
   8:  }

Then as we discussed in my earlier Code First blog posts (Entity Framework “Code First + Migration” 101 and Entity Framework “Code First + Existing Database” 102) – in the “Package Manager Console” window type “Add-Migration AddedComputedColumn” – where “AddedComputedColumn” could be any name you want to give this update — and then in the “Migrations” folder open the newly created migration script matching the name you gave it, for example, “201309021659377_AddedComputedColumn.cs” – commenting out what you see in line 5, with the updated command in line 6.
 

   1:  public partial class AddedComputedColumn : DbMigration
   2:  {
   3:      public override void Up()
   4:      {
   5:          //AddColumn("dbo.DemoRecords", "Created", c => c.DateTime());
   6:          AddColumn("dbo.DemoRecords", "Created", c => c.DateTime(defaultValueSql: "GETDATE()"));
   7:      }
   8:          
   9:      public override void Down()
  10:      {
  11:          DropColumn("dbo.DemoRecords", "Created");
  12:      }
  13:  }

And simple as that, in the “Package Manager Console” when you type “Update-Database” – you now have a auto-computed date/time field…  Rock on…

4 thoughts on “Entity Framework “Code First + Computed GETDATE()””

  1. Thanks for the information, I got it working!

    But..

    This is just sad. Very very sad. Simple? No, not simple. It’s simple compared to building a neural network but very complicated for just a tiny teeny computed column. Data migrations, for computed column??? EF=Entity Fail. I am considering learning NHibernate, but not sure if it’s going to be any better, some of my colleagues just use plain old-sql and maybe they have a big point!

    Thanks again for sharing your knowledge!

  2. Thanks for the information, I got it working!

    But..

    This is just sad. Very very sad. Simple? No, not simple. It’s simple compared to building a neural network but very complicated for just a tiny teeny computed column. Data migrations, for computed column??? EF=Entity Fail. I am considering learning NHibernate, but not sure if it’s going to be any better, some of my colleagues just use plain old-sql and maybe they have a big point!

    Thanks again for sharing your knowledge!

Leave a Reply