19 April 2016

Entity Framework and SQL Server's Time datatype

For a current WPF project, I needed to store a time value on SQL Server 2008, using its handy time datatype. For data retrieval, I was using Entity Framework 6 (EF), and my models had been built with database-first; hence the entity models in Visual Studio were automatically generated by EF.

Interestingly, EF used the .NET TimeSpan type to map to the SQL Server time datatype. My view models (for MVVM pattern) had to match the TimeSpan type as well. This all seemed strange, until I realized there's no .NET Time type. Regardless of the strangeness, EF successfully converted values between TimeSpan and SQL Server time, as documented in this article.