Entity Framework Stored Procedures and Complex Types

To call and use stored procedures from a database in Entity Framework is pretty easy. Stored procedures can return Scalars, Entities and also Complex Types (properties aggregated) as we will see here.

In this example we add an existing stored procedure called sp_GetArtistNameAndCountryById. We do this by refreshing the model from the database and select the stored procedure to be related to the store model.

Entity Framework Stored Procedure Update

After that we open the Model Browser and right click the store procedure name in the store and select ‘Add Function Import’…

Entity Framework Model Browser Stored Procedure

In the next dialog we add the stored procedure as a function to the conceptual model. Name the method and specify it to return a complex type. Press the ‘Get Column Information’ button to retrieve the returned properties. Also click the ‘Create New Complex Type’ to return as our new special Artist name-country combination.

Entity Framework Add Function Import Dialog

If we look at the Model Browser now, we see the Function Import for the GetArtistNameAndCountryById function in the conceptual model.

Entity Framework Model Browser Function Import

We write a test to see if it works. We retrieve an Artist with Id = 6 which happens to be the most wonderful Irish singer Luka Bloom. Note that the resultArtist is our complex type (GetArtistNameAndCountryById_Result).

using System.Data.Objects;
using System.Diagnostics;
using System.Linq;
using Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
 
namespace MusicEntitiesTests
{
    [TestClass]
    public class ArtistTest
    {
        [TestMethod]
        public void CallingGetArtistNameAndCountryByIdReturnsArtists()
        {
            using (var context = new MusicModelContainer())
            {
                ObjectResult<GetArtistNameAndCountryById_Result> result =
                    context.GetArtistNameAndCountryById(6);
                GetArtistNameAndCountryById_Result resultArtist = result.First();
 
                Debug.WriteLine("{0} from {1}",
                                resultArtist.Name,
                                resultArtist.Country);
 
                // Id 6 is Luka Bloom
                Assert.IsTrue(resultArtist.Name.Equals("Luka Bloom"));
            }
        }
    }
}

And the test has passed ones again :-)

Entity Framework Stored Procedure Unit Test

3 thoughts on “Entity Framework Stored Procedures and Complex Types

  1. Really good article.. Very well described. I was banging my head to get a clearcut implementation of the same.. Thanks a lot..:)

  2. That is great, but is there a way to same thing if only part of the columns returned is a complex type and the rest are scalar. Both the complex type and scalar values makes an entity in the model? I am new to the EF and I have read elsewhere that this can not be done

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>