1

I would like to implement some sort of test environment for ASP.Net projects. I am trying to implement this as an alternative to mocking the entity framework.

Here is what I am trying to achieve :

  1. Two DBContext classes are used, one for development, one for tests.
  2. These two DBContext classes will generate 2 databases through Enable-Migration/Update-Migration. Again, one for development, one for tests.
  3. Adding a new migration must be applied to both the development environment and the test environment.

Basically, the development database and test database must be identical. Therefore, my goal is to find a way so that we only have to run each type of command once to control both environments :

  • Enable-Migrations creates two migration configuration classes.
  • Add-Migration can be applied to the two environments.
  • Update-database applies the migrations on both environments.

Here is my attempt first attempt at the context classes :

namespace Project.DAL
{

    public class Context : DbContext
    {
        public Context(string connectionString) : base(connectionString) { }
        public Context() : base() { }

        public DbSet<User> Users { get; set; }
        public DbSet<Team> Teams { get; set; }
        public DbSet<Pool> Pools { get; set; }
        public DbSet<Match> Matches { get; set; }
        public DbSet<Invitation> Invitations { get; set; }
    }

    public class Dev : Context
    {
        public Dev() : base("project_dev") { }
    }

    public class Test : Context
    {
        public Test() : base("project_test") { }
    }
}

How would you guys approach this? I am coming from the Ruby On Rails framework, which has a development/test environment similar to what I am looking for.

This is with the code-first approach. So ideally, if one were to create a new project, he designs his models and applies migrations to populate/create two databases representing your development and test environments.

  • Are the two databases identical? Just change the connection string. – Robert Harvey Oct 6 '15 at 20:39
  • @RobertHarvey I've forgot to mention something important : this is with the code-first approach. So ideally, if one were to create a new project, he designs his models and applies migrations to populate/create two databases representing your development and test environments. – Corb3nik Oct 6 '15 at 20:43
  • Yes, that would be the idea. Have two identical databases, one EF context, two connection strings. – Robert Harvey Oct 6 '15 at 20:44
  • @RobertHarvey In that case, what do you mean exactly by changing the connection string? There might be something that I didn't explain properly... – Corb3nik Oct 6 '15 at 20:49
  • I would use database snapshots – Ewan Oct 6 '15 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.