How to configure LocalDb for unit tests in Visual Studio 2012 and Entity Framework 5

We have an ASP.NET MVC Visual Studio 2012 project using Entity Framework 5.

There are several unit tests that are database dependent. Setting up the app.config file in a test project to use a central SQL Server database is fine.

However, it would be much better to use LocalDb so that each developer has their own database when running the tests. Especially since we would like the tests to be run at DropCreateDatabaseAlways at startup.

However, I cannot get this installation to work. If I try this in app.config:

 <add name="TestDb" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=unittestdb; Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\unittestdb.mdf" providerName="System.Data.SqlClient" /> 

I get:

System.Data.SqlClient.SqlException: A file activation error occurred. The physical file name '\ unittestdb.mdf' may be incorrect. diagnose and fix additional errors, and repeat the operation. CREATE a database crash. Some file names cannot be created. Check for errors.

It looks like he wants the mdf file to already exist, which seems strange as he is trying to create a database. Creating the mdf file manually does not change the error message.

+57
unit-testing entity-framework-5 visual-studio-2012 mstest localdb
Sep 03 '12 at 8:38
source share
5 answers

Try:

 AppDomain.CurrentDomain.SetData( "DataDirectory", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "")); 

This will create a Db file at / bin / Debug / yourdbname.mdf

+32
Feb 04 '13 at 5:31
source share

I would use:

 // Declare this property - this is set by MSTest public TestContext TestContext { get; set; } // In test initialization - note the signature should be exactly this // A static void method with one argument of type TestContext [ClassInitialize] public static void SetUp(TestContext context) { AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(context.TestDeploymentDir, string.Empty)); } 

You may have problems using AppDomain.CurrentDomain.BaseDirectory , use: context.TestDeploymentDir instead

+9
Mar 08 '13 at 15:52
source share

Keep in mind that for a test project:

 AttachDBFilename = | DataDirectory |

means it will look in your output folder / bin / debug for unit test, and not in the App_Data folder in your web / production / whatever application.

You need to do two things 1. Move the OUT database file of your App_Data folder to the root of your test application. 2. Select your database to get the properties window in Visual Studio. Set the assembly action to β€œContent” so that it is copied to your output folder when the project starts.

Voila.

+3
Nov 19 '13 at 21:20
source share

I suggest using this code (based on Jupaol answer):

 [ClassInitialize] public static void SetUp(TestContext context) { AppDomain.CurrentDomain.SetData( "DataDirectory", context.TestDeploymentDir); } 

Usually this will create your database inside the TestResults\<test run>\Out\ folder of your solution.

+1
Nov 24 '13 at 15:38
source share

I found your question while searching for the answer to the problem. Using EntityFramework with nUnit in a separate project, I had to change App.config

looked like this:

 <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> </configuration> 
0
Oct 21 '14 at 9:49 on
source share



All Articles