MVC的异步,Entity Framework的异步,ADO.NET的异步,

时间:2022-07-25 23:40:26

MVC的异步化改造

无比轻松,只要把ActionResult改为async Task<AstionResult>:

public async Task<ActionResult> SiteHome(int? pageIndex)
{
//...
}
 

Entity Framework的异步化

也很轻松,查询时只需使用异步LINQ:

MVC的异步,Entity Framework的异步,ADO.NET的异步,

public async Task<int> GetAsync()
{
return await Entities
.Where(...)
.Select(...)
.CountAsync();
}

MVC的异步,Entity Framework的异步,ADO.NET的异步,

保存时只需SaveChangesAsync():

async Task IUnitOfWork.CommitAsync()
{
await base.SaveChangesAsync();
}

 

ADO.NET的异步化

所有进行异步化的数据库操作都需要用类似下面的ADO.NET代码进行改造

MVC的异步,Entity Framework的异步,ADO.NET的异步,

using(var conn = new SqlConnection(connectionString))
{
using(var command = conn.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "...";
command.Parameters.AddWithValue("...", ...);
await conn.OpenAsync();
using (IDataReader reader = await command.ExecuteReaderAsync())
{
//...
}
}
}

MVC的异步,Entity Framework的异步,ADO.NET的异步,

 

原文地址:http://www.cnblogs.com/cmt/p/aspnet_async_await.html