{"id":1314,"date":"2013-09-18T09:54:00","date_gmt":"2013-09-18T09:54:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2013\/09\/18\/scaffolding-asynchronous-mvc-and-web-api-controllers-for-entity-framework-6\/"},"modified":"2022-08-09T05:26:15","modified_gmt":"2022-08-09T12:26:15","slug":"scaffolding-asynchronous-mvc-and-web-api-controllers-for-entity-framework-6","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/scaffolding-asynchronous-mvc-and-web-api-controllers-for-entity-framework-6\/","title":{"rendered":"Scaffolding asynchronous MVC and Web API controllers for Entity Framework 6"},"content":{"rendered":"<h2>Brief<\/h2>\n<p>Entity Framework 6 introduces support for the .NET 4.5 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/data\/jj819165.aspx\">asynchronous programming pattern<\/a>&nbsp;using the async and await keywords. And in Visual Studio 2013 RC we&rsquo;re making it easier for you to take advantage of this new capability by optionally generating asynchronous code when you scaffold MVC and Web API controllers.<\/p>\n<h2>Why<\/h2>\n<p>First thing first. Why would you build an asynchronous controller? Well, asynchronous programming is an important part of building scalable, robust, and responsive web applications.<\/p>\n<p>A web server has a limited number of threads available, and in high load situation all of the available threads might be in use. When that happens, the server can&rsquo;t process new requests until the threads are freed up. With synchronous code, many threads may be tied up while they aren&rsquo;t actually doing any work because they&rsquo;re waiting for I\/O to complete. With asynchronous code, when a process is waiting for I\/O to complete, its thread is freed up for the server to use for processing other requests. As a result asynchronous code enables server resources to be used more efficiently, and the server is enabled to handle more traffic without delays.<\/p>\n<p>For more information about asynchronous programming, see the tutorial <a href=\"http:\/\/www.asp.net\/mvc\/tutorials\/mvc-4\/using-asynchronous-methods-in-aspnet-mvc-4\">Using Asynchronous Methods in ASP.NET MVC 4<\/a><a title=\"\" href=\"#_edn2\">[ii]<\/a>&nbsp;and the video <a href=\"https:\/\/channel9.msdn.com\/Events\/TechEd\/NorthAmerica\/2013\/DEV-B337#fbid=zFSkpWSceNP\">How to Build ASP.\/NET Web Application Using Async<\/a><a title=\"\" href=\"#_edn3\">[iii]<\/a>.<\/p>\n<h2>How<\/h2>\n<p>The MVC and Web API controller scaffolders have a new <strong>Use async controller actions <\/strong>check box.<br \/>The selection will be remembered next time you use the scaffolder.<\/p>\n<p><em>MVC Controller using Entity Framework<\/em><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/6888.fig1_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/6888.fig1_.png\" alt=\"\" border=\"0\" \/><\/a><br \/>&nbsp;<br \/><em><\/em><\/p>\n<p><em>Web API 2 Controller \/ OData Controller using Entity Framework<\/em><\/p>\n<p> <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/1106.fig2_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/1106.fig2_.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<h2>What<\/h2>\n<p>Following is the sample code generated by the scaffolders. The scaffolders generate code to read, create, update and delete data, using the Entity Framework data context.<\/p>\n<p><strong>Read<\/strong><\/p>\n<p><strong><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/8078.fig3_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/8078.fig3_.png\" alt=\"\" border=\"0\" \/><\/a><\/strong><\/p>\n<p>When you select the async option, the method is marked as &ldquo;async&rdquo;, the EF async API is called, and the &ldquo;await&rdquo; keyword is used when calling the EF API.<\/p>\n<p><strong>Create<\/strong><\/p>\n<p><strong><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/1462.fig4_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/1462.fig4_.png\" alt=\"\" border=\"0\" \/><\/a><\/strong><\/p>\n<p><strong>Update<\/strong><\/p>\n<p><strong><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/2474.fig5_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/2474.fig5_.png\" alt=\"\" border=\"0\" \/><\/a><\/strong><\/p>\n<p><strong>Delete<\/strong><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/1385.fig6_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/09\/1385.fig6_.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<div>\n<hr align=\"left\" size=\"1\" width=\"33%\" \/>\n<div>\n<p><a title=\"\" href=\"#_ednref1\">[i]<\/a> <a href=\"http:\/\/msdn.microsoft.com\/en-us\/data\/jj819165.aspx\">http:\/\/msdn.microsoft.com\/en-us\/data\/jj819165.aspx<\/a><\/p>\n<\/div>\n<div>\n<p><a title=\"\" href=\"#_ednref2\">[ii]<\/a> <a href=\"http:\/\/www.asp.net\/mvc\/tutorials\/mvc-4\/using-asynchronous-methods-in-aspnet-mvc-4\">http:\/\/www.asp.net\/mvc\/tutorials\/mvc-4\/using-asynchronous-methods-in-aspnet-mvc-4<\/a><\/p>\n<\/div>\n<div>\n<p><a title=\"\" href=\"#_ednref3\">[iii]<\/a> <a href=\"https:\/\/channel9.msdn.com\/Events\/TechEd\/NorthAmerica\/2013\/DEV-B337#fbid=zFSkpWSceNP\">https:\/\/channel9.msdn.com\/Events\/TechEd\/NorthAmerica\/2013\/DEV-B337#fbid=zFSkpWSceNP<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Brief Entity Framework 6 introduces support for the .NET 4.5 asynchronous programming pattern&nbsp;using the async and await keywords. And in Visual Studio 2013 RC we&rsquo;re making it easier for you to take advantage of this new capability by optionally generating asynchronous code when you scaffold MVC and Web API controllers. Why First thing first. Why [&hellip;]<\/p>\n","protected":false},"author":434,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[31,7333,7288],"class_list":["post-1314","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-asp-net","tag-asp-net-mvc","tag-asp-net-mvc-framework"],"acf":[],"blog_post_summary":"<p>Brief Entity Framework 6 introduces support for the .NET 4.5 asynchronous programming pattern&nbsp;using the async and await keywords. And in Visual Studio 2013 RC we&rsquo;re making it easier for you to take advantage of this new capability by optionally generating asynchronous code when you scaffold MVC and Web API controllers. Why First thing first. Why [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/1314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/434"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=1314"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/1314\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=1314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=1314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=1314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}