How to generate stored procedures as async methods through EF in c# application?

对着背影说爱祢 提交于 2020-01-30 10:53:25

问题


I've bunch of SPs that are used for invocation from my c# console application. So, I'm using EF "data base first" approach, which is rather convinient for me, because EF generates SP invokation code by itself and I'm not bothered with writing sql code like "EXEC sp...", wrapping my parameters and so on. The only problem is the next: EF generates synchronous versions of methods. And I'm wondering if there is a way to tell EF to generate asynchronous methods? Do we have any alternative framework that doing such thing? Desired pseudo-code looks something like that:

public async virtual Task<int> InvokeAsync(Nullable<byte> first, string second)
{
   var firstParameter = first.HasValue ?
       new ObjectParameter("CrawlerID", first) :
       new ObjectParameter("CrawlerID", typeof(byte));

   var secondParameter = second != null ?
       new ObjectParameter("JsonData", second) :
       new ObjectParameter("JsonData", typeof(string));

return await ObjectContext.ExecuteFunctionAsync("GetInt", firstParameter, secondParameter);
}

回答1:


Your DbContext represents the model of a database. It hides for you how the database actually is called.

Stored procedures are part of your database. Your DbContext should provide a function to call the stored procedure, while hiding that a stored procedure is used to perform the function. In other words: users of your DbContext don't care whether the function is performed using a query or SQL execute, or using a stored procedure. It could be a SQL statement and later be changed into a stored procedure without users having to know that it is changed.

Calling a stored procedure is done using DbContext.DataBase.ExecuteSqlCommand. There is an async awaitable function DbContext.DataBase.ExecuteSqlCommandAsync

class MyDbContext : DbContext
{
    ... // DbSets

    public async Task<MyResult> DoSomethingAsync(...)
    {
         object[] params = new Object[]
         {
             new SqlParameter(@"ParamX", ...),
             new SqlParameter(@"ParamY", ...);
         }
         const string SqlCommand = @"Exec MyStoredProcedure
             @ParamX,
             @ParamY);
         return await this.Database.ExecuteSqlCommandAsync(sqlCommand, params);
    }

Similarly, if you don't want to call a Stored Procedure but a LINQ query on DbSets you can use the async extension functions of DbSet

var result = await dbContext.MyItems
    .Where(item => ...)
    .GroupBy(item => ...)
    .OrderBy(group => ...)
    .FirstOrDefaultAsync();

Note, that as that use deferred execution only will change the Expression of the IQueryable. It is the function without deferred execution (ToList, Any, First, Max, ...) that will cause the Provider to execute the Expressions. So only those function need an async version



来源:https://stackoverflow.com/questions/47771964/how-to-generate-stored-procedures-as-async-methods-through-ef-in-c-sharp-applica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!