Ministry of Technology
Show Menu

Entity Framework Repositories - now on nuget

We've been working with T4 templates for use with Entity Framework for a couple of years now. T4 templates are great because they actually let you write code to generate code so you can say goodbye to a load of unnecessary boilerplate.

We really like Entity Framework as an ORM tool, but where it's always fallen down has been in an ability to easilly unit test the applications that use it (particularly back when we started, before the common usage of POCOs and DbContext). In order to mitigate these problems we began work on developing a T4 template that would auto generate a repository pattern infrastrucre from the EDMX file generated by Entity Framework. You can then use the repositories for accessing data, a Repository Factory for accessing the repositories themselves and maintaining save state and all with built in interfaces that can be easilly mocked. This also allows you to add alternative data sources to the same model by adding additional classes. 

Unit testing is a straightforward matter of then mocking the Repository Factory interface and then adding the needed repository mocks for each testing layer. All of the autogenerated classes are rendered as Partial Classes, so you can easilly add custom functionality in addition to the default generated methods of...

  • ByID()
  • All()
  • Add()
  • Delete()
  • Edit()

We've now done a new version of the template that should work with DbContext and POCOs in pretty much the same way (although it's easier to test the POCO approach, I still prefer the repository layer for data source abstraction).

The ObjectContext version is pretty solid and there is a version out now for EF 4.3.1, 5 and 6. The DbContext one hasn't been tested in anger yet, so any issues you may find please let us know - we've set up an issue tracker on BitBucket at

You can get the template from nuget - just search for 'Ministry' or check out the package pages here...

Using Entities (ObjectContext) with EF6

This is rather broken on the Microsoft side of things - if you upgrade an EDMX the code generation strategy still generates code using the wrong namespaces for EF 6. You can fix this by right clicking the EDMX surface, selecting 'Add Code Generation Item' and selecting 'EF6.x EntityObject Generator' - If it's not available locally it'll be available in the 'online' section. Marry this with the new EF6 ObjectContext Repo template and you should be good to go.