Entity Framework异步查询和保存
EF6開(kāi)始提供了通過(guò)async和await關(guān)鍵字實(shí)現(xiàn)異步查詢和保存的支持(.net 4.5及更高版本)。雖然不是所有的操作都能從異步中獲益,但是耗時(shí)的操作、網(wǎng)絡(luò)或IO密集型任務(wù)中,使用異步可以提升客戶端性能和增強(qiáng)服務(wù)器的擴(kuò)展性。
本文將覆蓋一下主題:
- 實(shí)例演練異步操作
- 創(chuàng)建模型
- 創(chuàng)建同步程序
- 改為異步操作
實(shí)例演練異步操作
下面演練將通過(guò)對(duì)比,很容易的觀察異步操作和同步操作,該演練目的不是說(shuō)明何時(shí)才是異步操作的關(guān)鍵場(chǎng)景。
創(chuàng)建模型
下面使用CodeFirst的流程創(chuàng)建模型并生成數(shù)據(jù)庫(kù),不過(guò)異步方法可以很好的工作于所有EF模型,包括EF設(shè)計(jì)器生成的模型。
創(chuàng)建一個(gè)控制臺(tái)應(yīng)用程序AsyncDemo。
添加EntityFramework NuGet包到項(xiàng)目中。
添加Model.cs到項(xiàng)目中,代碼如下:
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 }?
創(chuàng)建同步程序
有了EF模型,下面通過(guò)代碼模擬數(shù)據(jù)庫(kù)存取。
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 }上面代碼通過(guò)調(diào)用PerformDatabaseOperations()?保存一個(gè)Blog對(duì)象到數(shù)據(jù)庫(kù)中,然后從數(shù)據(jù)庫(kù)中檢索所有Blog,并顯示到控制臺(tái),然后顯示一行文本”Quote?of?the?day“。
由于上面程序是同步執(zhí)行的,所有可以觀察到程序按下面流程執(zhí)行:
?
改造為異步操作
對(duì)上面程序加以修改,使用async和await關(guān)鍵字實(shí)現(xiàn)異步操作。
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 }現(xiàn)在程序變?yōu)楫惒綀?zhí)行,可以觀察到異步執(zhí)行順序?yàn)?#xff1a;
轉(zhuǎn)載于:https://www.cnblogs.com/from1991/p/5425263.html
總結(jié)
以上是生活随笔為你收集整理的Entity Framework异步查询和保存的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ios APP开发简单实例
- 下一篇: bzoj1452: [JSOI2009]