Repository navigation
Open
Labels
Milestone
Description
From @rpundlik on March 22, 2017 12:9
While retrieving the results using stored procedure how can I retrieve and store multiple result set in view model in .net core
For e.g. from stored procedure I am returning records for below queries
Select * LMS_Survey
Select * from LMS_SurveyQuestion
Select * from LMS_SurveyQuestionOptionChoice
and below is view model
public class LMS_SurveyTraineeViewModel
{
public LMS_SurveyDetailsViewModel SurveyDetailsViewModel { get; set; }
public LMS_SurveyQuestionsViewModel SurveyQuestionsViewModel { get; set; }
public LMS_SurveyQuestionOptionChoiceViewModel SurveyQuestionOptionChoiceViewModel { get; set; }
}
This is how I am executing the stored procedure
public List<LMS_SurveyTraineeViewModel> GetTraineeSurvey(int surveyID)
{
try
{
List<LMS_SurveyTraineeViewModel> modelList = new List<LMS_SurveyTraineeViewModel>();
modelList = dbcontext.Set<LMS_SurveyTraineeViewModel>().FromSql("LMSSP_GetTraineeSurvey @surveyID = {0},@LanguageID = {1}", surveyID, AppTenant.SelectedLanguageID).ToList();
return modelList;
}
catch (Exception ex)
{
throw ex;
}
}
How can stored the multiple result set using stored procedure in view model ?
Right now I need to call the three separate stored procedures to get data and merging them into one view model
Thanks for the help !
Copied from original issue: aspnet/Mvc#6011
Reacted by turenc, albertosilva, Aliaksandr Kukrash, Vlad Iliescu, Daniele Mancini, Sergey Koshelev, KaushaL ™, Ivan Kiselychnyk, Luke Teng, Tihomir Iliev and 75 more
Reacted by Mh. Saleem Al-Zayat, Arnaud Valensi and SarathMohandas
Reacted by Mikael Dúi Bolinder, Alexis Gureev, Kasun Koswattha, Arnaud Valensi, Cristian Berlingó, z.gh and Patryk
Activity
Eilon commentedon Apr 11, 2017
I think this question is more about retrieving multiple result sets from a sproc, and less so about MVC view models.
Ricky-G commentedon Apr 12, 2017
@Eilon it looks there was an issue raised in the past #6026 but it was closed for some reason. Is there any current plan to include this feature in the 2.0 milestone.
Eilon commentedon Apr 12, 2017
@RickyG-Akl , the friendly folks in this repo will give you an answer 😄
[-]Return multiple resultset from stored procedure in .net core[/-][+]Support multiple resultsets[/+]ajcvickers commentedon Apr 12, 2017
@rpundlik @RickyG-Akl There isn't currently any support for this when using EF and FromSql. The workaround is to drop down to ADO.NET and use the DbConnection directly to run the query and handle the multiple resultsets. Putting this issue on the backlog so that we can consider adding support in the future.
ajayvikas commentedon Aug 29, 2017
@ajcvickers In EF6 we have ObjectContext.Translate api, which we could use in this kind of scenario. There it was possible to materialize SqlDataReader into the EF entity. However this is missing in EF Core 2.0. The issue has been raised in #4675. We use this feature extensively and without this we won't be able to migrate from EF6 to EF Core.
divega commentedon Aug 29, 2017
@ppN2, I am curious about how you are using
Translate(). Can you confirm if you need to have readers with multiple results?ajayvikas commentedon Sep 1, 2017
Generally when the entities are simple for example order, orderdetails etc kind, we don't need to use Translate. The include() method works just fine. There are many cases where we need to return a complex object graph from the stored procedure. Generally this stored procedure will have complex business rules and will return multiple resultsets (in our case about 15-20 resultsets). In these cases we run the ExecuteReader command and load SqlDataReader and using translate apis to dematerialize the entities one by one.
ajayvikas commentedon Sep 20, 2017
@ajcvickers, @divega By looking at past responses and the source code, we came up with the following procedure which is equivalent to the EF6 ObjectContext.Translate (We could extend it to include the merge options, similar to EF6 Translate provides). We have tested this code and seems to be working. We do not have deep knowledge of he source code, so our confidence level is not very high. Is it possible for somebody with deeper knowledge to review this code? This will really help us to move forward with the migration to EF Core 2.0.
The way to use this code will be
ErikEJ commentedon Sep 21, 2017
@sjh37 FYI!
sjh37 commentedon Sep 21, 2017
Multiple result sets from a sproc are supported via the Entity Framework Reverse Poco Generator. This generates code for EF6, which you could copy and put into your EF.Core project.
Download at https://marketplace.visualstudio.com/items?itemName=SimonHughes.EntityFrameworkReversePOCOGenerator
ErikEJ commentedon Sep 21, 2017
@sjh37 Notice that ObjectContext is not present in EF Core
41 remaining items
yogeshfirke07 commentedon Mar 30, 2023
i am getting error at RegisterStreamedResults method
i am using Microsoft.EntityFrameworkCore -Version 6.0.15
Nefcanto commentedon May 29, 2023
roji commentedon May 29, 2023
Nefcanto commentedon May 30, 2023
roji commentedon May 30, 2023
Nefcanto commentedon May 30, 2023
DarkAurore commentedon Jul 5, 2023
Thanks @mwgolden and @ArnaudValensi for this solution.
Just adding a small comment, if ever you have nullable enums in your models, you will get an exception, and the following code will help to cast and set the value properly in that case :
Misiu commentedon Jul 28, 2023
Another variation that allows getting paginated data and total count using a single query:
With the above method I can query database like this:
The extensions method uses data annotation on the model, so database columns are matched to the suitable properties.
The missing part is the usage of converters declared per property and globally.
Improvements are more than welcome!
dlwennblom commentedon Oct 5, 2023
The solutions above seem like a lot of extra work to me, and limits things to 1, 2 or 3 result sets.
Instead, I first created a simplified extension method (based upon what I saw above, except without the asynchronous logic):
and then called it in a foreach loop for each result set (4 results in my case):
Usage:
var dashboard = new QueryStoredProcedureWithMultipleResults(dbContext, parm1, parm2, parm3);It works for me in Entity Framework Core 7.0.11 and is very similar to the ((IObjectContextAdapter)dbContext).ObjectContext.Translate approach from Entity Framework 6.2 (and the .Net Framework).
RickSandstrom commentedon Oct 5, 2023
@dlwennblom This seems like a more elegant solution. I will maybe change to this one in the future
Misiu commentedon Oct 5, 2023
@dlwennblom thank you for your version. Indeed, it looks cleaner.
My version allows me to handle complex pagination scenarios. In my case, I'm using CTE and temporary tables to filter data according to permissions, so I wanted to avoid doing the same quest two times to get the total number of records and then get paginated records. My code allows getting both results at once, but I'm sure the same can be done using your approach.
Charles113 commentedon May 23, 2024
Is a very slow Solution. Also it would trigger all Property Setters, including prop changed and other stuff.
EF Internally uses Cached Delegates, IndexMaps and the mysterious
_shapermethod.I would suggest to use
IRuntimePropertyBase.GetSetter()for theIClrPropertySetter.Which is probably a lot faster.
An official Solution would be nice.
Or just a method call to map a DbDataReader to a simple list of objects.
We don't need a full solution. Just open the API a little bit.
The advantage of
ctx.Database.SqlQueryis that it also does auto includes, tracking and other stuff.