Search code examples
entity-frameworknugetsql-server-cenuget-packagebootstrapping

How to create bootstrapper class for Entity Framework 4.3 & SQL Server Compact 4.0


(TL;DR version is the question in bold at the bottom)

When Entity Framework 4.1 Code First was originally released (actually it was around CTP5 time), there was a NuGet package called EFCodeFirst. This could be used in conjunction with another NuGet package called EFCodeFirst.SqlServerCompact which allowed the use of EF with SQL Server Compact 4.0 (SQL Server CE).

Installing the EFCodeFirst.SqlServerCompact package would scaffold, or generate a "bootstrapper" class called SQLCEEntityFramework.cs within an App_Start folder which would setup things like EF's Database.DefaultConnectionFactory along with generating "stubs" for other useful methods (initializing, seeding the db etc.) by using the WebActivator package

These two packages (EFCodeFirst & EFCodeFirst.SqlServerCompact) are now deprecated, and appear to have been replaced by equivalent packages of EntityFramework & EntityFramework.SqlServerCompact and both are currently at version 4.3.1

My problem is that, using these new packages in a new project, they do not seem to create the "bootstraper" class in order to set-up EF to use SQL Server Compact at run-time. Sure, I can just copy an old version of the class from a previous project, but it would be nice to know that the NuGet package still contained this functionality (that would potentially be updated over time).

Is there any way to get the latest version (4.3.1 at the time of this writing) of the EFCodeFirst.SqlServerCompact package to create the SQLCEEntityFramework.cs bootstrapper class, either upon installation or after the fact (perhaps via a powershell command?) ?

UPDATE:
I've just noticed that a previous version of the EFCodeFirst.SqlServerCompact NuGetPackage still has a dependency upon the WebActivator package (which is leveraged by the bootstrapper class) however this dependency has been removed from both the latest version and the one immediately preceding the latest version. Looks like the functionality that generates the bootstrapper class has been removed, but why I wonder?


Solution

  • In EF 4.1, the only way to make Code First create a SQL Server Compact database by convention (i.e. without explicitly specifying a connection string) was to set the DefaultConnectionFactory in code. This is problematic because tooling (such as Migrations) that needs to know what database is being used does not know that this code is present.)

    In EF 4.3 we added the ability to do this in the web.config file. The EntityFramework.SqlServerCompact package makes use of this by setting the SQL Conmpact as the default connection factory in your web config. It therefore does not need to use WebActivator or use code to set this.

    You can still use the code method if ypu want to, but be aware that things like Migrations may not work correctly and we would encourage you to use the config file instead.