Last active
November 5, 2019 09:12
-
-
Save sdanna/1a2853fbffa914c0f8fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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