EF6開始提供了經過async和await關鍵字實現異步查詢和保存的支持(.net 4.5及更高版本)。雖然不是全部的操做都能從異步中獲益,可是耗時的操做、網絡或IO密集型任務中,使用異步能夠提高客戶端性能和加強服務器的擴展性。數據庫
下面演練將經過對比,很容易的觀察異步操做和同步操做,該演練目的不是說明什麼時候纔是異步操做的關鍵場景。服務器
下面使用CodeFirst的流程建立模型並生成數據庫,不過異步方法能夠很好的工做於全部EF模型,包括EF設計器生成的模型。網絡
建立一個控制檯應用程序AsyncDemo。異步
添加EntityFramework NuGet包到項目中。async
添加Model.cs到項目中,代碼以下:性能
1 using System.Collections.Generic; 2 using System.Data.Entity; 3 4 namespace AsyncDemo 5 { 6 public class BloggingContext : DbContext 7 { 8 public DbSet<Blog> Blogs { get; set; } 9 public DbSet<Post> Posts { get; set; } 10 } 11 12 public class Blog 13 { 14 public int BlogId { get; set; } 15 public string Name { get; set; } 16 17 public virtual List<Post> Posts { get; set; } 18 } 19 20 public class Post 21 { 22 public int PostId { get; set; } 23 public string Title { get; set; } 24 public string Content { get; set; } 25 26 public int BlogId { get; set; } 27 public virtual Blog Blog { get; set; } 28 } 29 }
有了EF模型,下面經過代碼模擬數據庫存取。spa
1 using System; 2 using System.Linq; 3 4 namespace AsyncDemo 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 PerformDatabaseOperations(); 11 12 Console.WriteLine(); 13 Console.WriteLine("Quote of the day"); 14 Console.WriteLine(" Don't worry about the world coming to an end today... "); 15 Console.WriteLine(" It's already tomorrow in Australia."); 16 17 Console.WriteLine(); 18 Console.WriteLine("Press any key to exit..."); 19 Console.ReadKey(); 20 } 21 22 public static void PerformDatabaseOperations() 23 { 24 using (var db = new BloggingContext()) 25 { 26 // Create a new blog and save it 27 db.Blogs.Add(new Blog 28 { 29 Name = "Test Blog #" + (db.Blogs.Count() + 1) 30 }); 31 db.SaveChanges(); 32 33 // Query for all blogs ordered by name 34 var blogs = (from b in db.Blogs 35 orderby b.Name 36 select b).ToList(); 37 38 // Write all blogs out to Console 39 Console.WriteLine(); 40 Console.WriteLine("All blogs:"); 41 foreach (var blog in blogs) 42 { 43 Console.WriteLine(" " + blog.Name); 44 } 45 } 46 } 47 } 48 }
上面代碼經過調用PerformDatabaseOperations() 保存一個Blog對象到數據庫中,而後從數據庫中檢索全部Blog,並顯示到控制檯,而後顯示一行文本」Quote of the day「。.net
因爲上面程序是同步執行的,全部能夠觀察到程序按下面流程執行:線程
對上面程序加以修改,使用async和await關鍵字實現異步操做。設計
1 using System; 2 using System.Data.Entity; 3 using System.Linq; 4 using System.Threading.Tasks; 5 6 namespace AsyncDemo 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 var task = PerformDatabaseOperations(); 13 14 Console.WriteLine("Quote of the day"); 15 Console.WriteLine(" Don't worry about the world coming to an end today... "); 16 Console.WriteLine(" It's already tomorrow in Australia."); 17 18 task.Wait(); 19 20 Console.WriteLine(); 21 Console.WriteLine("Press any key to exit..."); 22 Console.ReadKey(); 23 } 24 25 public static async Task PerformDatabaseOperations() 26 { 27 using (var db = new BloggingContext()) 28 { 29 // Create a new blog and save it 30 db.Blogs.Add(new Blog 31 { 32 Name = "Test Blog #" + (db.Blogs.Count() + 1) 33 }); 34 Console.WriteLine("Calling SaveChanges."); 35 await db.SaveChangesAsync(); 36 Console.WriteLine("SaveChanges completed."); 37 38 // Query for all blogs ordered by name 39 Console.WriteLine("Executing query."); 40 var blogs = await (from b in db.Blogs 41 orderby b.Name 42 select b).ToListAsync(); 43 44 // Write all blogs out to Console 45 Console.WriteLine("Query completed with following results:"); 46 foreach (var blog in blogs) 47 { 48 Console.WriteLine(" - " + blog.Name); 49 } 50 } 51 } 52 } 53 }
如今程序變爲異步執行,能夠觀察到異步執行順序爲: