Checking out one of the new stored procedure features in EF4

I certainly have not yet explored the depths of EF4 to the same extent as I have with the current version and I have a lot of discoveries ahead of me.

Thanks to a tweet this morning by Zekq, I finally saw one of the promised improvements to EF’s stored procedure support.

In EF v1, you can easily create a function based on a stored procedure through function mapping for stored procs that return scalars. But calling those functions is not as easy. The code generator does not create a class method for the function. The only way to execute these functions is through EntityClient. You can easily find examples of this on the web or if you have my book, Programming Entity Framework, there is an example in VB and C# at the beginning of Chapter 13.

Now in EF4, the default code generator does indeed create the function so that it is now simple to call the function in your code without spinning up and executing an EntityConnection and EntityCommand.

If you haven’t mapped functions before, here is a quick walkthrough:

When you import a stored proc with teh EDM Wizard (or Update Wizard), the procedure gets inserted into the store schema (SSDL) but not into your conceptual model.

functionmappinga

You need to map the procedure to your CSLD using a Function Import.

Step 1: Right click on the function in the model browser and choose Add Function Import.

 

functionmappingb

Step 2: Define the Function Import. Here I have indicated that the function returns an Int32 result.

functionmappingb1 

Now the function is part of your CSDL. You will not see it in the designer but you will see it in the raw XML and the Model Browser.

functionmappingc

So far this is exactly the same as EFv1. The difference is that the code generator has add the function to the EFWorkshopEntities class and I can easily call it in my code now as I would any other function.

 functionmappingd

#1 shankar(india) on 7.22.2009 at 3:24 PM

Excellent Article. Will you be covering EF4 in your book version 2 and when is it being released? I use oracle 10g with 2000 entities and EF1 was difficult plumbing for stored procedures.

#2 Julie on 7.22.2009 at 5:15 PM

@shankar - no comment about book version 2 at the moment. Still recovering from v1 ;-)

#3 zeqk on 7.23.2009 at 8:22 AM

I really wait for EF 4. I use a return scalar value function import for a custom insert without autoincrement PK

#4 Julie on 7.23.2009 at 8:30 AM

I know it's more code but for now, you could create a custom method for your objectContext that executes the EntityClient code on your behalf.. just an idea.

Leave a Comment