zoukankan      html  css  js  c++  java
  • Entity Framework 6.0 Tutorials(2):Async query and Save

    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 == 1).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:

    async query output

    Download sample project for Async query & save demo.

  • 相关阅读:
    副业收入是我做程序员的2倍!副业这么有 “钱”景,我要考虑转行吗?
    C语言丨const关键字的用法详解
    C/C++学习笔记:C/C++函数调用的方式,你应该要学会这五种
    软件崩溃了,该如何解决? 解决问题的关键要会对症下药!
    C语言丨深入理解volatile关键字
    C语言丨getch(),getche()和getchar()的区别
    学编程的误区——眼高手低,不重视练习!
    通过编写“猜测数字”游戏来探索Linux中的Bash
    零基础想要更快入门Linux?找对方法,让你少奋斗10年!
    VS/VC 出现闪退怎么办?这4个技巧要知道!
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5649510.html
Copyright © 2011-2022 走看看