This is my first foray into the Entity Framework, and I have a working project with EF5 and a repository template. I want to conduct integration testing with a live database. I took a snapshot of my existing production database and wrote a stored procedure to recreate a new snapshot every time I want to run tests. My question is how to switch my context to this database snapshot when "in unit test mode"? In my app.config, I have both my live and test connection strings as such:
<connectionStrings> <add name="ReportingDbContext" connectionString="Server=LiveServer;Database=UnifiedReporting;User Id='myuser';Password='mypass';Trusted_Connection=False" providerName="System.Data.SqlClient" /> <add name="TestingDbContext" connectionString="Server=LiveServer;Database=UnifiedReportingSnapshot;User Id='myuser';Password='mypass';Trusted_Connection=False" providerName="System.Data.SqlClient" /> </connectionStrings>
As of now, I have my own DbContext with objects that I want to use as follows:
public class ReportingDbContext : DbContext { public ReportingDbContext() : base("name=ReportingDbContext")
I think I need to change the base ("name = ReportingDbContext") to ("name = TestingDbContext"), but given how I have my repository / UnitOfWork setup, I donβt see how I can do this, The problem may be here in my UnitOfWork:
public interface IUnitOfWork : IDisposable { void Commit();
This UnitOfWork was great, so I can do a bunch of things for all my repositories and save it with one shot, without having a bunch of contexts floating around for synchronization. This may or may not be relevant for this question, but this is how my UnitOfWork uses the repository. There is only 1 repository class, but it can be loaded with any type of entity:
public interface IRepository<T> where T : class { IQueryable<T> GetAll(); IQueryable<T> Find(Expression<Func<T, bool>> predicate); T GetById(int id); void Remove(T entity); void Add(T newEntity); } public class Repository<T> : IRepository<T> where T : class { protected DbContext DbContext { get; set; } protected DbSet<T> DbSet { get; set; } public Repository(DbContext dbContext) { if (dbContext == null) { throw new ArgumentNullException("dbContext"); } DbContext = dbContext; DbSet = DbContext.Set<T>(); } public IQueryable<T> GetAll() { return DbSet; }
The endpoint for using this magic is inside my WCF service. Here I want to actually pass the integration test. In my service, a special method initializes a unit of work and uses it. UnitOfWork creates a ReportingDbContext when it is updated, and this ReportingDbContext, in turn, refers to the connection string "name = ReportingDbContext". After a lot of reading, I think the answer is to use an IoC container such as Unity or Ninject (not previously used, but I would like to), and I was fixated on how to implement IoC in this situation. Here is an example that I use in my WCF service, which seems pretty tightly tied to the database connection string:
public ComputerDTO GetComputerDetails(string hostname, string client) {
I would like to keep my connection strings inside my app.config, if at all possible, and be able to somehow switch to the connection test string during the [SetUp] part of my testing of NUnit methods in my WCF service.