Last post May 10, 2020 01:46 AM by bruce (sqlwork.com)
May 07, 2020 06:43 PM|hamed_1983|LINK
My question is that how can i use stored procedure in ef core 3.1 to return something like anonymous types without creating some viewModel as dbSet in our dbContext ?
Thanks in advance
May 07, 2020 08:11 PM|bruce (sqlwork.com)|LINK
EF core 2.1+ binds only bind to known query types in the dbcontext. from these query types you can project to an anonymous object via a select. as you cannot pass anonymous types from the controller to the view this typically isn't a hardship.
if you you don't want to use EF core binding, then use a data reader. the dbcontext allows access to connection objct
using var context = new SampleContext();
using var command = context.Database.GetDbConnection().CreateCommand();
command.CommandType = CommandType.StoredProcedure
command.CommandText = "procname";
// add any parameters
using var reader = command.ExecuteReader();
// map to any object
May 08, 2020 04:18 PM|hamed_1983|LINK
Thanks for reply
Indeed, i want to pass my returned data from my proc as model to my view.
Apparently, as you told i should create a viewModel for my return type of proc to pass to the view. that's true?
May 09, 2020 08:05 PM|jimap_1|LINK
Here is a good article:
Stored procedures are used.
May 10, 2020 01:46 AM|bruce (sqlwork.com)|LINK
if you using EF, then you you are probably interested in using typed datasets. this means you should define classes for sp result sets.