Automatic Generation of Stored Procedure Return Types

Published 12 November 09 12:07 PM | dpblogs 

A new feature of the Entity Framework in .NET 4.0 is the ability to return collections of complex type instances from stored procedures. In the next public release of the designer, we have enriched this functionality by adding the ability to automatically create these complex types by querying stored procedure metadata from the database server. To demonstrate this feature, we will use the Northwind database, and we will focus on a stored procedure called “CustOrdersDetail”. Looking at the designer’s model browser window after we reverse engineer Northwind, we see:

Untitled

Double-clicking “CustOrdersDetail” brings up the “Add Function Import” dialog:

Untitled

We click “Get Column Information” in order to see the metadata returned by the server about this stored procedure’s return columns. Then, we click on the “Create New Complex Type” button, and the designer automatically creates a new complex type that matches the shape of the data returned from the stored procedure:

Untitled

To make life more interesting (and realistic,) let’s rename some of the properties in the generated complex type: ProductName to Name, and UnitPrice to Price. To keep things running, we then need to fix the function import’s mappings to look like this:

Untitled

Finally, to simulate some real-world churn, we change the stored procedure’s return shape: We’ll rename “UnitPrice” to “UnitPricing”, add a new column called “Foo”, and delete “Quantity”:

ALTER PROCEDURE CustOrdersDetail @OrderID int
AS
SELECT ProductName,
UnitPricing=ROUND(Od.UnitPrice, 2),
Discount=CONVERT(int, Discount * 100),
Foo = '!',
ExtendedPrice=ROUND(CONVERT(money, Quantity * (1 - Discount) * Od.UnitPrice), 2)
FROM Products P, [Order Details] Od
WHERE Od.ProductID = P.ProductID and Od.OrderID = @OrderID

That done, we double click on the function import again, get column information, and this time, click on the “Update” button. The designer will now inspect the mappings, the existing complex type, and the new return shape, and produce these proposed changes to the complex type:

Untitled

As you can see, “Foo” will be added, and “Quantity” will be deleted. In addition, the mappings are used to map “Name” to “ProductName” meaning no action will be taken for it. Finally, since “UnitPricing” is seen as new, “Price” will be deleted and “UnitPricing” will be put in its stead. Since we do not want this, we will rename the column in the function import’s mapping to “UnitPricing” and rerun the above process. This time we get:

Untitled

Which is what we would want.

In conclusion, the designer now exposes the Entity Framework’s new ability to return complex type instances from stored procedure. On top of this, it adds some features to make this capability easier to use and to iterate over. In future releases of Visual Studio, we will improve the iterative features and look into supporting batch imports of stored procedures, so that you will not need to import them one by one. We look forward to your feedback.

Noam Ben-Ami
Program Manager, Entity Designer

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Ken Cox [MVP] said on November 12, 2009 12:34 PM:

This is fine, but the Choose Your Database Objects page in the wizard really threw me. I expected that if I checked the "Stored Procedures" item on the Add tab, the wizard would import and map the stored procedures for me as it does with tables and fields.

It took a while to figure out that I also needed to manually add the stored procedures as functions using Add Function Import. Perhaps this could be clarified in a tooltip or something?

# Dhananjay Goyani said on November 12, 2009 10:44 PM:

Wow, this looks very nice feature.

# Tobi said on November 14, 2009 6:31 AM:

There does to seem to be a way to specify a EntitySet name. Are the results of fuction imports still limited to a single result?

Regards

Toby.

# Samuel said on November 16, 2009 4:37 AM:

That will be great feature in EF 4 . But I wonder how to make complex types automatically if stored procedure return multiple result shapes.

In my system, there are bunch of legacy stored procedure code which return a different result shapes

depends on input parameters. I wonder how i can overcome this situation using EF4.

# kjopc said on November 26, 2009 2:23 PM:

Silly question about how to rename properties on the generated complex type:

How do you get the Mapping Details dialogue box to go into edit mode for the first column? I can rename the Column column, but not anything in the Property column.

Ken

# Pooya said on January 1, 2010 8:32 AM:

Just wondering does it support returning multiple resultsets from the stored procedure? If it does, how do you configure it?

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker