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.

Entity Framework 6.0 Tutorials(2):Async query and Save的更多相关文章

  1. Entity Framework 6.0 Tutorials(1):Introduction

    以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...

  2. Entity Framework 6.0 Tutorials(4):Database Command Logging

    Database Command Logging: In this section, you will learn how to log commands & queries sent to ...

  3. Entity Framework 6.0 Tutorials(11):Download Sample Project

    Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...

  4. Entity Framework 6.0 Tutorials(10):Index Attribute

    Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...

  5. Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping

    Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...

  6. Entity Framework 6.0 Tutorials(6):Transaction support

    Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...

  7. Entity Framework 6.0 Tutorials(3):Code-based Configuration

    Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...

  8. Entity Framework 6.0 Tutorials(8):Custom Code-First Conventions

    Custom Code-First Conventions: Code-First has a set of default behaviors for the models that are ref ...

  9. Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange

    DbSet.AddRange & DbSet.RemoveRange: DbSet in EF 6 has introduced new methods AddRange & Remo ...

随机推荐

  1. 洛谷 P3225 [HNOI2012]矿场搭建

    传送门 题目大意:建设几个出口,使得图上无论哪个点被破坏,都可以与出口联通. 题解:tarjian求割点 首先出口不能建在割点上,找出割点,图就被分成了几个联通块. 每个联通块,建出口.如果割点数为0 ...

  2. spring_JavaConfig

    从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中. interface: package sprin ...

  3. ansible安装基本使用

    备注使用yum (centos7)   1. 安装 yum install -y ansible 2. 免密登录(ssh,最好使用dns 解析) // create ssh key ssh-keyge ...

  4. laravel 中使用定时任务

    Laravel5.3 Artisan Console 文档地址 http://laravelacademy.org/post/6228.html 1.在服务器上查看定时任务有哪些crontab -e ...

  5. sqlserver卸载

  6. vue-cli+webpack项目 修改项目名称

    使用vue-cli+webpack创建的项目,修改文件名称或者更改文件的位置,运营时会报错,是因为npm项目,在安装依赖(node_nodules)时,会记录当前的文件路径,当修改之后就无法正常启动. ...

  7. 微信小程序的基本认识

    小程序与公众号的区别 小程序,不支持关注,消息推送等营销手段. 小程序更倾向于产品,公众号更倾向于营销. 在系统权限方面,小程序能够获得更多. 小程序与APP的区别 小程序,面向微信用户.app面向所 ...

  8. PL/SQL 训练03 --异常

    --程序员在开发的时候,经常天真的认为这个世界是完美的,用户如同自己般聪明,总能按照自己设想的方式--操作系统输入数据.但残酷的事实告诉我们,这是不可能的事情,用户总会跟我们相反的方式操作系统--于是 ...

  9. MVC 公共类App_Code不识别

    .Net MVC需要写公共类的时候 右击添加 App_Code 文件夹,新建类—>右击类—>属性,生成操作 —>选择 —>编译 .net MVC项目本身是个应用程序,所以其实不 ...

  10. 工程添加EF框架的方法

    1.VS2015添加新项缺少ADO.net实体数据模型解决方法 手动运行安装目录包中的\packages\EFTools\EFTools.msi即可恢复 2.此时,在添加->新建项目下会出现AD ...