New Posts New Posts RSS Feed: Get non-entity data from stored proc using code first
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Get non-entity data from stored proc using code first

 Post Reply Post Reply
Author
jbiddle61 View Drop Down
Newbie
Newbie


Joined: 18-Dec-2012
Location: Arizona
Posts: 24
Post Options Post Options   Quote jbiddle61 Quote  Post ReplyReply Direct Link To This Post Topic: Get non-entity data from stored proc using code first
    Posted: 13-Sep-2013 at 1:19pm
My app is N-Tier using Code First.
This is working great for normal entity queries.
Some of our reports however use very complex stored procedures to return the results.
I really don't want to have to try to convert these to LINQ queries.

I've looked high and low, but I can't find any way to call a stored procedure on the server.
I've tried Remote Server Method Call, but I always get a "The FunctionImport 'StoredProcName' could not be found in the container 'DbContextName'.

I'm down to two things which I know will work, but I really hope there is a better way.

One is to use ADO.NET to invoke the stored proc and manually create my list of objects to return to the client.
I REALLY don't want to mix ADO.NET with EF.

The other is to create a second model project that uses database first and an EDMX file and only import the stored procs into that model.
This is not ideal but still better than using standard ADO.NET.
If I go that route, can I use the same DataSourceKeyName for both models since they point to the same database?

Anyone out there have ANY suggestions on the best approach?

Thanks,

      John

Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 16-Sep-2013 at 10:04am
If you have a DbContext, you should be able to call a stored procedure from that.  You can either access the context.Database property for database-specific calls (like SqlQuery or ExecuteSqlCommand), or cast the DbContext to an IObjectContextAdapter to obtain the underlying ObjectContext, and then use ExecuteStoreCommand, ExecuteStoreQuery or ExecuteFunction.  
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down