1

I am developing a WPF application which requires some database access. For development purposes I am using localdb with database migrations enabled and EF6.

What I cannot figure out is how do I configure a separate localdb database for integration tests inside a VS2013 test project and run the database migrations to bring it up to date and then seed it with data.

Alan Rutter
  • 321
  • 4
  • 16

2 Answers2

2

Here's what I've ended up doing based off information from several sources. First, in the App.Config of my test project I've added the following

<connectionStrings>
    <add name="YourContextName" connectionString="Data Source=(localdb)\mssqllocaldb;Integrated Security=true;AttachDBFilename=|DataDirectory|\databasename.mdf"
   providerName="System.Data.SqlClient" />    
</connectionStrings>

Then I created the following class:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.Entity;
using MyProject.Model.Entities;
using MyProject.Migrations;

namespace IntegrationTests
{
    public class DatabaseInitializer : DropCreateDatabaseAlways<MyProjectDataContext>
    {
    }

    [TestClass]
    public class Initalize
    {
        [AssemblyInitialize]
        public static void AssemblyInit(TestContext context)
        {
            AppDomain.CurrentDomain.SetData("DataDirectory", context.TestDeploymentDir);
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyProjectDataContext, Configuration>());

            //Database.SetInitializer(new DatabaseInitializer());
    }
}

}

My understanding is that this runs before each test run. The SetData call updates the |DataDirectory| used in the App.Config so that it is unique and doesn't try to use the same instance as the normal project.

From there I have the choice of either running the migrations and then supplying some test data as part of the test or to run an initializer that sets up the test data. Note that to run the migrations the configuration class generated by the migration must be changed from internal to public.

This seems to work so far. Not sure if this is the best way or if I can combine the migrations and then run a different seed for the tests.

Alan Rutter
  • 321
  • 4
  • 16
  • The only issue I have with this is that when I do a 'Run All' from the Test Explorer everything works fine. When I try to debug a test I get an error about attempting to attach an auto-named database. Regardless of which SetInitializer method I use, the mdf is not created in the folder for Debug. – Alan Rutter May 07 '15 at 23:51
0

Within your test project, you could set Database.SetInitializer() (or databaseInitializer within the project config file) with MigrateDatabaseToLatestVersion or with your own IDatabaseInitializer that calls DbMigrator.Update, and use a custom DbMigrationsConfiguration.

The test project could use a separate connection string for using LocalDB, and if the file referenced by your connection string's AttachDBFilename, then the initializer will try to create it.

Community
  • 1
  • 1
jjj
  • 4,822
  • 1
  • 16
  • 39