A couple of
months ago I blogged about an extension I wrote to execute a stored procedure more easily with Entity Framework CodeFirst. It certainly bumped up the number
of visitors I got on my blog, which I of course love to see, but it also made
me think of so many things that are missing in EF CF.
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!
NuGet package? :D
BeantwoordenVerwijderenNot a bad idea, I'll see if I can get to it one of these days :)
BeantwoordenVerwijderenNumber 4 is already possible when you reference System.Data.Entity :-)
BeantwoordenVerwijderenStefan, I wasn't aware of that, glad to hear!
BeantwoordenVerwijderenThis is a very good approach to executing SP using EF code first. I made a slight change by adding two extensions
BeantwoordenVerwijderenone for database.SqlQuery
one for database.ExecuteSqlCommand
the second one allows me to execute DDL/DML type database updates and return the number of rows affected
Thanks again for the initial idea
Jawwahar
I'm interested in seeing your extensions, can you do a pull request or send me the code? I might add them to the existing code.
VerwijderenHi Luc,
BeantwoordenVerwijderenI've got your code from github and got it working with a SQL Express database sproc:)
I'm now trying to get it working with an Oracle database sproc, but I am having problems.
My Oracle sproc takes one input paramater and returns the result in an out cursorRef parameter.
Do you know how I can get this working with my Oracle sproc? It seems to be complaining that the out cursorRef is not provided in the parameter list.
Any help appreciated.
Regards,
Paul.