I'm trying to add some integration tests for my database access logic using EF code-first 6.1.2. I'd like to do this on a separate database so that I don't mess up m production database with test data.
Also, the test should be repeatable which means the database should be created if not exists, seed test data, run the tests and finally delete it when done.
I don't see how this can be done without Enable-Migrations command for the test database too.
This would be my dbContext for production:
public partial class ApplicationDbContext :
IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>, IApplicationDbContext
{
public ApplicationDbContext() : base("name=DefaultConnection") { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (base.Database.Connection.ConnectionString == "DefaultConnectionTest")
Database.SetInitializer(new DropCreateDatabaseAlways<ApplicationDbContext>());
else
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("Users");
modelBuilder.Entity<ApplicationRole>().ToTable("Roles");
modelBuilder.Entity<ApplicationUserRole>().ToTable("UserRoles");
modelBuilder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
modelBuilder.Entity<ApplicationUserClaim>().ToTable("UserClaims");
}
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Person> Persons { get; set; }
}
I have everything set up using Dependency Injection from Ninject.
Adding an extra constructor with the connection string as parameter public ApplicationDbContext(string dbConnection) : base(dbConnection) { }, would still leave me to run the Enable-Migrations command.
Is there some way to automate this process? Or can anyone suggest something else?
I'd like to stick with SQL Server database as working with a different database for testing might give me false positives.