這個特性從EF6開始,在之前的版本中不存在.
EF6(.net 4.5)開始支持用async和await關鍵字進行異步查詢和保存,不是所有的程序都能從異步中得到好處,當被掛起長時間的運行,網絡或IO-bound任務,可以被用于提高客戶端的響應和服務端的擴展,
什么時侯適合使用asyn
創建model
創建異步程序
讓程序異步
結論
什么時侯適合使用asyn
這篇文章的目的是用一種方式介紹asyn概念,讓它很容易被觀察到異步和同步程序之間的區別,這個演練不關注異步編程帶來好處的任何場景。
異步操作通常用于執行完成時間可能較長的任務,如打開大文件、連接遠程計算機或查詢數據庫。異步操作在主應用程序線程以外的線程中執行。應用程序調用方法異步執行某個操作時,應用程序可在異步方法執行其任務時繼續執行。
在客戶端程序(WinForm, WPF等)中,在當前的線程執行時異步操作可用于保持UI的響應。在服務端程序中(ASP.NET等)異步線程可以被用于處理其它未來的請求 - 這是可以減少內存的使用量或增加服務器的吞吐量。
在多數的程序中使用異步沒有明顯的好處,甚至是有害的。使用測試,分析和通用場景來測量在你的特定的情況下的影響。
Create the model
創建控制臺程序AsyncDemo

添加EntityFramework Nuget package

安裝EntityFramework package

創建Model - AsyncDemo.cs:
using System.Collections.Generic;
using System.Data.Entity;
namespace AsyncDemo
{
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
}創建一個異步程序
class Program
{
static void Main(string[] args)
{
PerformDatabaseOperations();
Console.WriteLine();
Console.WriteLine("Quote of the day");
Console.WriteLine("Don't worry about the world coming to an end today... ");
Console.WriteLine("It's already tomorrow in Australia.");
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
public static void PerformDatabaseOperations()
{
using (var db = new BloggingContext())
{
// Create a new blog and save it
db.Blogs.Add(new Blog
{
Name = "Test Blog #" + (db.Blogs.Count() + 1)
});
db.SaveChanges();
// Query for all blogs ordered by name
var blogs = (from b in db.Blogs
orderby b.Name
select b).ToList();
// Write all blogs out to Console
Console.WriteLine();
Console.WriteLine("All blogs:");
foreach (var blog in blogs)
{
Console.WriteLine(" " + blog.Name);
}
}
}
}PerformDatabaseOperations方法會保存一個新的Blog到數據庫并且獲取所有的Blogs然后打印在控制臺中。

修改為異步
//添加Tasks引用
using System.Data.Entity;
using System.Threading.Tasks;
static void Main(string[] args)
{
var task = PerformDatabaseOperations();
Console.WriteLine("Quote of the day");
Console.WriteLine(" Don't worry about the world coming to an end today... ");
Console.WriteLine(" It's already tomorrow in Australia.");
task.Wait();
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
public static async Task PerformDatabaseOperations()
{
using (var db = new BloggingContext())
{
// Create a new blog and save it
db.Blogs.Add(new Blog
{
Name = "Test Blog #" + (db.Blogs.Count() + 1)
});
Console.WriteLine("Calling SaveChanges.");
await db.SaveChangesAsync();
Console.WriteLine("SaveChanges completed.");
// Query for all blogs ordered by name
Console.WriteLine("Executing query.");
var blogs = await (from b in db.Blogs
orderby b.Name
select b).ToListAsync();
// Write all blogs out to Console
Console.WriteLine("Query completed with following results:");
foreach (var blog in blogs)
{
Console.WriteLine(" - " + blog.Name);
}
}
}原文:https://msdn.microsoft.com/en-us/data/jj819165.aspx
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。