Get non-entity data from stored proc using code first
Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2012
Forum Discription: For .NET 4.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4319
Printed Date: 06-Sep-2025 at 6:51am
Topic: Get non-entity data from stored proc using code first
Posted By: jbiddle61
Subject: Get non-entity data from stored proc using code first
Date 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
|
Replies:
Posted By: kimj
Date 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.
|
|