There are times where you want to use a stored procedure that doesn’t return just normal rows out of a database.  One classic example is if you are doing Full-Text Searching and want to return the Rank.

The first step to doing this is the get your stored procedures exposed in the ADO.NET Entity Data Model.  Instead of going through the steps for doing that here, I’ll just point you to a great post by Julie Lerman on her blog: Implement SELECT Stored Procedures that return miscellaneous data in CTP2 of EF Designer.

After following those steps, you will have the stored procedures all ready to be called from the Entity Data Model.  You can then use these stored procedures from ASP.NET, ASP.NET MVC, or anything else that can consume the Entity Data Model.

One side note, if you use the “Update Model from Database…” feature on your Entity Model, it will destroy the store layer tables that you created from Julie’s post.  If that happens, just recreate them again.

Now to get this to work from Silverlight using RIA, there is another step that needs to be done.  You need to get these stored procedures exposed in your Domain Service.  One way to handle doing that is to create functions in the Domain Service like the following for each of them.  Assume you have a stored procedure called SearchData and it takes a string as input to seach for in the database.  You can create a function in the Domain Service like:

   1: public IQueryable<SearchDataTable> GetSearchData(String SearchString)
   2: {
   3:     return Context.SearchData(SearchString).AsQueryable();
   4: }

When you compile this, it will create a client side function called LoadSearchData that takes a string as input.  Then you can call this just like you load any other data in Silverlight RIA:

   1: public void Searching(String search)
   2: {
   3:     if (String.IsNullOrEmpty(search))
   4:     {
   5:         return;
   6:     }
   7:  
   8:     context.LoadSearchData(search);
   9:     context.Loaded += new EventHandler<System.Windows.Ria.Data.LoadedDataEventArgs>(context_Loaded);
  10: }
  11:  
  12: void context_Loaded(object sender, System.Windows.Ria.Data.LoadedDataEventArgs e)
  13: {
  14:     var context = sender as MyDomainContext;
  15:     foreach (SearchDataTable searchData in context.SearchDataTable)
  16:     {
  17:         ... do stuff with the data ...
  18:     }
  19: }

Notice that I am hooking up to when the context is finished being loaded so that I know the data has been populated.