Async query and Save:

You can take advantage of asynchronous execution of .Net 4.5 with Entity Framework. EF 6 has the ability to execute a query and command asynchronously using DbContext.

Let's see how to execute asynchronous query first and then we will see an asynchronous call to context.SaveChanges.

Asynchronous Query:

private static async Task<Student> GetStudent()
{
Student student = null; using (var context = new SchoolDBEntities())
{
Console.WriteLine("Start GetStudent..."); student = await (context.Students.Where(s => s.StudentID == ).FirstOrDefaultAsync<Student>()); Console.WriteLine("Finished GetStudent..."); } return student;
}

As you can see in the above code, GetStudent method is marked with async to make it asynchronous. The return type of asynchrounous method must be Task. GetStudent returns an object of Student entity so return type must be Task<Student>.

Also, query is marked with await. This frees the calling thread to do something else until it executes the query and returns the data. We have used FirstOrDefaultAsync extension method of System.Data.Entity. You may use other extension methods appropriately, such as SingleOrDefaultAsync, ToListAsyn, etc.

Asynchronous Save:

You can call context.SaveChanges asynchronously the same way as async query:

private static async Task SaveStudent(Student editedStudent)
{ using (var context = new SchoolDBEntities())
{
context.Entry(editedStudent).State = EntityState.Modified; Console.WriteLine("Start SaveStudent..."); int x = await (context.SaveChangesAsync()); Console.WriteLine("Finished SaveStudent...");
} }

Getting async query result:

You can get the result when asynchronous using the wait method as below:

public static void AsyncQueryAndSave()
{
var student = GetStudent(); Console.WriteLine("Let's do something else till we get student.."); student.Wait(); var studentSave = SaveStudent(student.Result); Console.WriteLine("Let's do something else till we get student.." ); studentSave.Wait(); }

As shown in the code above, we call async method GetStudent in the usual way and store the reference in the variable student. Then, we call student.wait(). This means that the calling thread should wait until the asynchronous method completes, so we can do another process, until we get the result from the asynchronous method.

The code shown above will have the following output:

Download sample project for Async query & save demo.

最新文章

  1. JavaScript数据存取的性能问题
  2. Elasticsearch初步使用(安装、Head配置、分词器配置)
  3. 判断一个Activity 判断一个包 是否存在于系统中 的方法
  4. mac 下配置 VS Code 开发 Golang
  5. js复选框操作
  6. pycharm active code
  7. 整理: Android HAL
  8. Linux内核树的建立-基于ubuntu系统
  9. C#.NET中的CTS、CLS和CLR
  10. windows 7多点触摸开发
  11. 淘宝npm镜像使用方法
  12. eclipse java生成exe
  13. Spring源码分析:Spring IOC容器初始化
  14. 自定义工作流活动运行产生System.Security.SecurityException
  15. Oracle 12c RAC 静默安装文档
  16. 错误:软件包:3:docker-ce-18.09.4-3.el7.x86_64 (docker-ce-stable) 需要:container-selinux &gt;= 2.9
  17. this与super
  18. mybatis的plugin
  19. Laravel 执行流程(一)之自动加载
  20. Sql 标识列 增长1000

热门文章

  1. Spring aop 简单示例
  2. 如何设计并使用FireMonkeyStyle
  3. mysql编译参数详解
  4. sql中case when的简单使用
  5. 分布式缓存系统 Memcached slab和item的主要操作
  6. 1009 Product of Polynomials
  7. maven学习2
  8. PHP字符串的处理(二)-字符串的格式化
  9. IE bug之location.href没有referer
  10. 25-从零玩转JavaWeb-抽象类