How to handle dataset containing multiple tables in Dotnet core using Fromsql ()
I am creating WebApi using net net core
I have created a procedure that returns data from 2 AS tables: -
select * from table1
select * from table2
I have a list of models as: -
public class ModelList
{
public model1 m1 { get; set; }
public model2 m2 { get; set; }
}
I follow the procedure and get the result in the mode list as: -
List<ModelList> ML= new List<ModelList>();
ML= db.ModelList.FromSql("EXECUTE [dbo].[usp]").ToList();
I am not getting the result of these two tables in these two separate entities.
it works with a single table, but not multiple.
suggest where am i doing the mistake?
or is it doable in a dotted kernel?
+3
source to share
1 answer
The following implementation will give the desired result:
public ModelList ExecuteMultiple(string query){
using( IDbConnection connection = new SqlConnection(YOUR_CONNECTION_STRING)){
try{
connection.Open();
var multi = connection.QueryMultiple(query);
var modelList = new ModelList();
modelList.m1 = multi.Read<model1>();
modelList.m2 = multi.Read<model2>();
return modelList;
}
catch( Exception ex){
Console.WriteLine(ex.Message);
}finally{
connection.Close();
}
}
return null;
}
Using:
var modelList = ExecuteMultiple("EXECUTE [dbo].[usp]");
0
source to share