Skip to content

Instantly share code, notes, and snippets.

@sdanna
Created October 23, 2015 21:40
Show Gist options
  • Save sdanna/e22bc99a7ebb59273eae to your computer and use it in GitHub Desktop.
Save sdanna/e22bc99a7ebb59273eae to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using Zomg.DAL;
namespace IntegrationTests.Fixtures
{
public class DbFixture<T> : IDisposable
{
private readonly string _connectionString;
public ApplicationDbContext Context { get; private set; }
public DbFixture()
{
var typeOfT = GetType().UnderlyingSystemType.GenericTypeArguments[0].FullName.Replace(".", "_");
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AppData");
AppDomain.CurrentDomain.SetData("DataDirectory", path);
if (Directory.Exists(path) == false)
Directory.CreateDirectory(path);
_connectionString = String.Format(@"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Zomg-{0}.mdf;Integrated Security=True;MultipleActiveResultSets=True", typeOfT);
Context = new ApplicationDbContext(_connectionString);
}
public void Dispose()
{
Context.Database.Connection.Close();
Context.Database.Delete();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment