Entity Framework Code First View Generation Templates On Visual Studio Code Gallery

Some time ago I created T4 templates for creating pre-generated views for Entity Framework Code First applications. I wanted to make them available as soon as possible so I just uploaded them as a zip file to one of my sites and provided a link. This worked as a short-term solution but long-term I wanted something better. Something that would not require manual work. Something that would integrate with Visual Studio seamlessly. Something that is actually called Visual Studio Code Gallery. And it happened yesterday. Yesterday I published the templates on the Visual Studio Code Galery.

Using the templates

First you need to download the templates. You can do it directly from Visual Studio. Right click on your project and select Add -> New Item (Ctrl+Shift+A). In the Add New Item dialog go to “Online templates”:

Add New Item - Online Templates

and search for “EF Views”. This should make the “EF CodeFirst View Generation T4 Template for C#/VB” show up (note: only the template for the language of the current project will show up).

Add New Item - Search Templates

Change the name of the file at the bottom to {Context}.Views.tt where {Context} is the name of the class derived from DbContext you want to create pre-generated views for.
Click “Add” to add the template to your project. Wait for the views to be generated (note: for bigger models view generation may take an extended amount of time).

You can also install templates manually just by downloading vsix files from Visual Studio Code Gallery and pressing “Enter”. Here are direct links to the templates:

Once you installed the templates you can find them in the “Code” category. Right click on your project and select Add -> New Item (Ctrl+Shift+A). In the “Add New Item” dialog go to the “Code” section:

Add New Item - Using Installed Templates

If needed the templates can be uninstalled from Extension Manager (Tools -> Extension Manager):

Unistalling Templates

Happy coding.

Pawel Kadluczka

Entity Framework Code First and Pre-generated Views

When working with Entity Framework view generation may take a long time for bigger or complicated models. The workaround for this problem is to use pre-generated views. In case of Database First and Model First approaches you can use T4 templates that will create the pre-generated views (you can find more details here). But what to do when using Code First approach? One possibility is to use Entity Framework Power Tools – just right-click on the file containing your DbContext derived class and select “Optimize Data Model”. Voila – views are created. But what if you need more control, cannot use UI (e.g. you want your build system to create the pre-generated views) or the tool for whatever reason does not work? You can for instance follow this 5 steps:

  • Get Edmx file for your model using EdmxWriter.WriteEdmx() method
  • Retrieve csdl, msl, ssdl from the edmx file and save them
  • From the Visual Studio Command Prompt run EdmGen with mode parameter set to ViewGeneration (i.e. /mode:ViewGeneration)
  • Add the generated C#/VB.Net file to your project
  • Repeat each time you change your model

Easy? Maybe. Error prone? Probably. Cumbersome? For sure. But now there is a third way. Similarly to the T4 templates for Model and Database First workflows I created T4 templates for creating pre-generated views for Code First approach. Now, you would just add the right (C# or VB.Net) template to your project, rename it so that it contains the name of the context you want to create the views for (e.g. MyContext.Views.tt) and generate your views by right clicking the template and selecting “Run Custom Tool” menu option. Note that creating the pre-generated views with the template will take approximately the same amount of time it takes to create views at runtime – you are just doing the work that would be done at runtime at design time. The templates are (temporarily) available here. I will update this post when they are moved to the final location.The templates are available on the Visual Studio Code Gallery. See this post for more details.

Pawel Kadluczka

Entity Framework Zombie (a.k.a. Microsoft Entity Framework June 2011 CTP)

Back in June 2011 we released a CTP called “Microsoft Entity Framework June 2011 CTP”. We were excited by all the features (enums, support for spatial types, TVFs, auto-compiled queries etc.) included in this preview and people were excited to try them. Unfortunately shipping a component that is part of .NET Framework outside the .NET Framework turned out to be challenging. This resulted in a number of tricks like the beloved 😉 by everyone “Microsoft Entity Framework June 2011 CTP” target or publisher’s policies with binding redirects and the experience was far from perfect (e.g. uninstalling Microsoft Entity Framework June 2011 CTP will break tools as noted in the Readme). Now this release is haunting people. We investigated a number of issues where the root cause of the problem was presence of this CTP on the machine. The issues are popping up out of nowhere and it may be hard to figure out what’s going on. Some of them are – Migrations not working correctly (http://stackoverflow.com/questions/9860097/ef-5-vs-11-error-when-doing-add-migration), MissingMethodExceptions (http://stackoverflow.com/questions/9591929/can-anyone-spot-why-i-keep-getting-this-error-testing-the-ef-5-beta) , Visual Studio 11 Beta crashing (this is actually another flavor of MissingMethodException but you probably won’t even see it without WinDbg). Why this happens? The .NET Framework 4.5 is an in-place update. Microsoft Entity Framework June 2011 CTP installed binding redirect to redirect calls to System.Data.Entity.dll 4.0.0.0 to System.Data.Entity.dll 4.2.0.0. This redirect *will still work* if you install .NET Framework 4.5 (be it Beta, RC or RTM). This is bad as there were quite a few changes we added after the June CTP was shipped and dependencies were taken on these changes (e.g. EF Designer). Once .NET Framework 4.5 Beta shipped there is not any benefit of having the CTP – what’s in CTP is in .NET Framework 4.5 Beta, the CTP does not come with any support (as opposed to .NET Framework 4.5 Beta), the CTP does not have a “go-live” license (as opposed to .NET Framework 4.5 Beta and Entity Framework 5 Beta). Even though I believe Microsoft Entity Framework June 2011 CTP was a very useful release *it’s time to move on and uninstall it*. You can find instructions here: http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx.

Pawel Kadluczka

Entity Framework and DateTime of Unspecified kind

I generally consider DateTimeKind.Unspecified dangerous or maybe even broken therefore I was really surprised one day when I saw that some code generation templates in the Entity Framework actually use Unspecified DateTimeKind. I wanted to check what happens during materialization and, sure enough, the materialized DateTime values that came from EF were of unspecified kind. It seemed weird to me and I wanted to understand why this was happening. To my own surprise I concluded that this behavior (at least for Sql Server – I did not check other databases) actually made sense. Here is why:
DateTime properties can be mapped to columns of the following types (again this is for SqlServer):

  • date
  • smalldatetime
  • datetime
  • datetime2

Neither of these types is aware of nor store information about time zones. What it means for date time objects of Utc or Local kind is that the information about their kind is actually lost when the data is persisted in the Sql Server. Now, when the values are read from the SqlServer they will be of Unspecified type (because SqlServer does not store information about time zones and this is the default kind). To understand what it really means you need to take a look at how conversions to Local and Utc times work for DateTime values of Unspecified kind. Basically, if you call DateTime.ToLocalTime() the date is assumed to be a Utc date and if you call DateTime.ToUniversalTime() the date is assumed to be a local date (more details here: http://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime.aspx, http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx). In practice it looks like this:

Source Date                           .ToLocalTime()          .ToUniversalTime()
12/12/2000 10:58:49 AM (Unspecified)  12/12/2000 2:58:49 AM   12/12/2000 6:58:49 PM
12/12/2000 10:58:49 AM (Utc)          12/12/2000 2:58:49 AM   12/12/2000 10:58:49 AM
12/12/2000 10:58:49 AM (Local)        12/12/2000 10:58:49 AM  12/12/2000 6:58:49 PM

Based on this table let’s take a look what happens if you write a Utc date value to the database. When I store the following date in the database: 12/12/2000 10:58:49 AM (Utc) and then read it I will get: 12/12/2000 10:58:49 AM (Unspecified). It may seem that these dates are the same they actually are not – the Utc values for these dates are respectively: 12/12/2000 10:58:49 AM and 12/12/2000 6:58:49 PM. The first conclusion is that dates of non-Uspecified kind actually *don’t roundtrip*. The second conclusion is that dates of Unspecified kind *only roundtrip if they are saved and read and used in the same time zone*. The final conclusion is that if your app uses dates that need to be time zone aware (and in today’s highly connected world it probably applies to the vast majority of applications) you should use properties of DateTimeOffset type which will be mapped to columns of datetimeoffset type that is aware of time zone information.

Pawel Kadluczka