We are moving to Entity Framework, however we have a large number of stored procedures. If our Web API uses an async/await pattern all the way to the stored procedure call, how do I handle the call? Can I/should I make it async as well?
I have a db context and I can call my stored procedure Foo synchronously. This works however the calling code is all wrapped with async/await keywords. The stored procedure in question is also a Select and not an Insert/Update/Delete. I cannot use ToListAsync() on the context.Foo call, if I try
context.Database.ExecutSQLCommandAsync()
I only get a Task<int> back and not my object. Is there a best practice here?
public async Task<List<FooData>> GetFoo(string param)
{
List<FooData> dataList = new List<FooData>();
using(dbEntities context = new dbEntities(connectionstring))
{
// this runs as expected
var result = context.Foo(param).ToList();
if(result != null)
{
result.ForEach(delegate(Foo_Result item)
{
// load object
});
}
}
return dataList;
}
I suppose I could do this
var result = await Task.Run(()=> context.Foo(param).ToList());
but this is an Entity Framework db call and not an CPU intensive process. Is there a best practice to follow here? Thanks