I am trying to call a stored procedure via EF 6. Using ToList() works as expected and a list of entities is returned. However, ToListAsync() does NOT seem to return data. In sql Profiler, I can see the stored procedure being executed against the database. However, the break point after the line for ToListAsync() does not get hit. I never see data get returned.
code below
public async Task<List<MyEntityObject>> GetStoredProcedureData() {
List<MyEntityObject> MyEntityObjects;
using (var dbContext = new DbContext())
{
var MyEntityObjectsQry = dbContext.Database.SqlQuery<MyEntityObject>("dbo.GetStoredProcedureData");
MyEntityObjects =await MyEntityObjectsQry.ToListAsync();
}
return MyEntityObjects;
}
I figured this out. I was not consuming the method correctly. I was using a windows service. Plus, I could not add async to the Main method.
static void Main(string[] args)
{
// async cannot be added to Main
// await CallStoredProcedure(); will not work here
Task<bool> task = Task.Run<bool>(async () => await CallStoredProcedure());
var test = task.Result;
}
static async Task<bool> CallStoredProcedure()
{
var dataService = new MyDataService();
// just call my method with await
var details = await dataService.GetStoredProcedureData();
Console.WriteLine("found {0} items", details.Count);
return true;
}
The get method will be like
var myEntityObjectsQry = await dbContext.Database.ToListAsync();
More references here: https://www.entityframeworktutorial.net/entityframework6/async-query-and-save.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.