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. npm 私服工具verdaccio 安装配置试用

      1. 安装 npm install -g verdaccio 2. 启动 verdaccio // 界面显示信息 Verdaccio doesn't need superuser privileg ...

  2. new Date(2016,3,29,10) 时区的问题

    http://my.oschina.net/xinxingegeya/blog/394821http://www.cnblogs.com/qiuyi21/archive/2008/03/04/1089 ...

  3. bzoj 4650 & 洛谷 P1117 优秀的拆分 —— 枚举关键点+后缀数组

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4650 https://www.luogu.org/problemnew/show/P1117 ...

  4. (装)Android杂谈--禁止TimePicker控件通过keyboard输入

    Android 4.1版本以上用的是类似与ios的滚动时间控件,但是4.1以下,用的TimePicker确实通过点击上下按钮来更改时间的,虽然也提供了编辑框编辑,但是可能会超出编辑范围 如果要禁止编辑 ...

  5. LinkedList插入排序实现

    昨天遇到一个集合排序的问题,要求在list中插入后数据有序,首先考虑使用集合自带的排序方法,但需要把list转成数组,排序后再转回list.后来发现使用插入算法是最省事的,因为既然是在插入里排序,那么 ...

  6. nginx学习资源

    在了解nginx的时候 看到的一些资源: https://www.cnblogs.com/EdwinChan/p/8350984.html http://tengine.taobao.org/book ...

  7. Linux 配置静态Ip地址

    注:所有红色字体标注均为 Linux 的 操作命令 ! 1, 使用root账户登录系统 2, 可以先使用 ifconfig : 查看网卡信息   eth0 为默认的第一个网卡 , 如果有第二个就会显示 ...

  8. mysql复制原理与机制一

    复制原理:复制需要二进制日志记录数据库上的改变 slave的IO线程复制把master上的Binary log读取到本地的relay log里SQL线程负责把relay log恢复到数据库数据里 sh ...

  9. Office 2019 2016 安装破解教程

    声明:工具由蓝点网提供支持,密钥为本人收集内容,非转载部分 GVLKs for Office 2019     Product GVLK Office Professional Plus 2019  ...

  10. 1050 String Subtraction

    题意:给出两个字符串s1和s2,在s1中删去s2中含有的字符. 思路:注意,因为读入的字符串可能有空格,因此用C++的getline(cin,str).PAT系统迁移之后C语言中的gets()函数被禁 ...