
During my
time spend on a project that uses EF CF, I’ve bundled some extensions I wrote
to facility my needs and to make developing a whole lot easier. It surprises me that the Entity Framework
team didn’t ship these features in their release.
Below you’ll
find an explanation for every extension I wrote. The code is on github, so feel free to make
any additions as you like.
1. Executing Stored Procedures
I already
wrote an extensive blog post about this one so feel free to read it here.
To summarize, the extension allows you to execute
a stored procedure as so:
var testProcedureStoredProcedure = new TestProcedureStoredProcedure() { Iets = 5, NogIets = true };
var result = DbContext.Database.ExecuteStoredProcedure(testProcedureStoredProcedure);
I’ve made a small addition
that now you can decide which class will be used to resolve the stored
procedure name: the result class or the stored procedure class.
2. Map
to custom dto’s with ESQL
By default
you can only map to entities when using ESQL. If you write your select
statement explicitly then you get a DbDataRecord. This is a PIA when customizing you selects to
minimize data transfer. The extension I
wrote can map all selected fields to your dto.
var query = _unitOfWork.CreateQuery<DbDataRecord>(queryString);var dtos = query.Map<YourDTO>();
3. Retrieve the DbSet with a generic
The
datacontext object of EF CF exposes a number of DbSet’s. I found it quit useful to be able to
generically address these DbSet’s. So I’ve
come up with the following solution.
With this extension you can select a DbSet by supplying the type of the entity
as a generic.
dataContext.GetDbSetReference<YourEntityType>();
4. Include with lamda
Why on
earth they didn’t include this in version 1.0 I have no idea but unless you’ve
already wrote your own extension for it (probably!), I’ve included a way to do this
in the extensions project also.
Basically it enables you to write:
EntitySet.Include(x => x.NavigationProperty)
So that’s about it. You can find the code on github. As I’ve said, I’m happy to accept any useful additions to the code base.
Have fun
coding, till next time!