Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Boilerplate for an assembly initialization class to map the datadirectory and reset the local db for unit testing in C# using Entity Framework
[TestClass]
public class Initalize
{
[AssemblyInitialize]
public static void InitializeDbContext(TestContext context)
{
// Connection string in app.config should use LocalDb, EG:
// <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\DefaultConnection.mdf;Initial Catalog=DefaultConnection;Integrated Security=True" providerName="System.Data.SqlClient" />
AppDomain.CurrentDomain.SetData("DataDirectory", context.TestDeploymentDir);
Database.SetInitializer(new DropCreateDatabaseAlways<ApplicationDbContext>());
// Load test data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment