Skip to content

Instantly share code, notes, and snippets.

@sdanna
Last active November 5, 2019 09:12
Show Gist options
  • Save sdanna/1a2853fbffa914c0f8fd to your computer and use it in GitHub Desktop.
Save sdanna/1a2853fbffa914c0f8fd 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;
private static Checkpoint _checkpoint = new Checkpoint
{
TablesToIgnore = new[]
{
"sysdiagrams",
"__MigrationHistory"
},
DbAdapter = DbAdapter.SqlServer
};
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 RespawnDatabase()
{
_checkpoint.Reset(_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