Click here to Skip to main content
13,248,087 members (36,584 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 3 Apr 2012

Using NUnit and Entity Framework DbContext to Programmatically Create SQL Server CE Databases and Specify the Database Directory

, 3 Apr 2012
Rate this:
Please Sign up or sign in to vote.
How to use NUnit and Entity Framework DbContext to programmatically create SQL Server CE databases and specify the database directory.

I won't go into the why but I wanted to write what probably amounts to an integration test of an Entity Framework model. I decided to use NUnit for this. Even though it's predominately for Unit Testing, there's no reason why it can't be used for other forms of testing. In fact, I think the SpecFlow BDD framework eventually performs its tests using NUnit: It's not what you've got but what you do with it!

The application I'm writing uses SQL Express but for my tests, I wanted to create & destroy a database frequently and locally so I opted to use SQL Server Compact Edition. This is pretty easy to do with EF's DbContext: just create an instance of it, pass in either the name of the database or a Connection String, e.g.:

public void TestWithConnectionString()
  var db = new DbContext("Name=Foo");

which references the following Connection String in the .config file:

  <add name="Foo"


  connectionString="Data Source=foo.sdf"/>

As I'm going to be potentially creating many of these, I wanted precise control over their name and location. This meant I didn't really want to use a Connection String as this is more or less hard-coded in the .config file.

Instead, I opted to create it programmatically. By default, DbContext expects to be using SQL Server in particular an instance of SQLExpress named .\SQLExpress. This is easily changed by replacing the DefaultConnectionFactory with the factory class for SQL Server CE: SqlCeConnectionFactory.

The first parameter to the constructor is the type of the factory class which allows instances of different versions to be created. This is simply the providerName as per the Connection String. The second parameter is the directory to create any new databases in. The third parameter (unused in the example below) are options to append to the generated Connection String. The code below will result in the creation of C:\UsersPete\TestDBs\bar.sdf assuming the directory exists and the user has appropriate permissions and bar.sdf does not already exist.

public void TestWithFactory()
  Database.DefaultConnectionFactory = 
  new SqlCeConnectionFactory(
  @"C:\Users\Pete\TestDBs\", "");

  var db = new DbContext("bar.sdf");


That's mainly it: A simple way to programmatically create databases. This code should really be moved to the SetUp method and a corresponding TearDown method added to delete the database; oh, and some tests!

I haven't got that far yet and the actual reason for writing the entry was for a short while despite specifying the directory for the databases, i.e., C:\Users\Pete\TestDBs they were being created in the current working directory. This was because rather than just specifying the name of the database as the argument to DbContext, I was passing the string "DataSource=bar.sdf" of which the name part appears to be an absolute path and thus override that specified in the SqlCeConnectionFactory. As only a file name is present, it uses the current working directory for the path element. If you've got the same issue (whether integration testing or not), this might save you a little time.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader
United Kingdom United Kingdom
My day job is mostly working in C++ with a bit of C#. I write a fair amount of command line based tools and really wish they could have a GUI front-end to them hence why I spend my spare time working with WPF.

I started a blog few years back but didn't do a lot with it. I've started describing some of the interesting programming things I come across on it. Please take a look.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 3 Apr 2012
Article Copyright 2012 by PeteBarber
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid