Search for existing suggestions

Make Entity Framework fully mockable and injectable

In general, mocking out the Entity Framework DAL is a big pain to accomplish. ObjectContext has no interface and the T4 templates aren't written to create interfaces for the objects they create.

This hampers good unit testing and makes them less injectable.

Put an interface on ObjectContext. Have the T4 Templates put interfaces around all of the objects that are generated and make sure that all objects used have interfaces so that mocking frameworks, like MOQ or Moles & Pex can automatically generate stubs to replace the objects in unit tests so that tests don't need to be tightly coupled to the database.

Marking as completed as we have made advances in EF Core towards this (and we did previously in EF6) and we don’t plan to go any further.

In general we recommend against trying to mock EF API behaviors beyond simple cases because they are too complex and dependent on the underlying providers and databases. Instead we recommend either using in-memory databases or creating abstractions such as repositories in applications, which can be more easily mocked.