{"id":303,"date":"2012-04-24T12:06:00","date_gmt":"2012-04-24T12:06:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/cesardelatorre\/2012\/04\/24\/entity-framework-code-first-dbcontext-and-sql-azure-connection-fault-handling\/"},"modified":"2012-04-24T12:06:00","modified_gmt":"2012-04-24T12:06:00","slug":"entity-framework-code-first-dbcontext-and-sql-azure-connection-fault-handling","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/entity-framework-code-first-dbcontext-and-sql-azure-connection-fault-handling\/","title":{"rendered":"Entity Framework Code First DbContext and SQL Azure Connection Fault Handling"},"content":{"rendered":"<p><img decoding=\"async\" width=\"244\" height=\"72\" title=\"image\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/7446.image_thumb_444062C6.png\" border=\"0\" \/>&nbsp; <a href=\"http:\/\/blogs.msdn.com\/imgres?imgurl=http:\/\/www.scip.be\/ImagesLogos\/Small\/Article_EntityFramework.jpg&amp;imgrefurl=http:\/\/www.scip.be\/index.php%3FPage%3DArticles%26Lang%3DEN%26Category%3DEntity%2520Framework&amp;usg=__UHLSnJ6EkcIvRjbYUlnagIY2Qq4=&amp;h=101&amp;w=135&amp;sz=11&amp;hl=es&amp;start=2&amp;zoom=1&amp;itbs=1&amp;tbnid=MRZOAWQhPnckQM:&amp;tbnh=69&amp;tbnw=92&amp;prev=\/images%3Fq%3DMicrosoft%2BEntity%2BFramework%26hl%3Des%26sa%3DG%26gbv%3D2%26tbs%3Disch:1\"><img decoding=\"async\" width=\"92\" height=\"69\" src=\"http:\/\/t2.gstatic.com\/images?q=tbn:MRZOAWQhPnckQM:\" \/><\/a><\/p>\n<p>I&rsquo;ve been working migrating an App to <em>Windows Azure<\/em> and <em>SQL Azure<\/em>. This app is using <strong>Entity Framework 4.3 Code First<\/strong> and <strong>DbContext<\/strong>.<\/p>\n<p>One important point is related to the SQL Azure Conection Fault Handling. If you don&rsquo;t know about this topic, you can read this explanatory info:<\/p>\n<p><a title=\"http:\/\/social.technet.microsoft.com\/wiki\/contents\/articles\/1541.sql-azure-connection-management-en-us.aspx\" href=\"http:\/\/social.technet.microsoft.com\/wiki\/contents\/articles\/1541.sql-azure-connection-management-en-us.aspx\">http:\/\/social.technet.microsoft.com\/wiki\/contents\/articles\/1541.sql-azure-connection-management-en-us.aspx<\/a><\/p>\n<p>Then, regarding a workaround when using Entity Framework 4.0 (based on ObjectContext), I wrote this post a few months ago:<\/p>\n<p><a title=\"http:\/\/blogs.msdn.com\/b\/cesardelatorre\/archive\/2010\/12\/20\/handling-sql-azure-connections-issues-using-entity-framework-4-0.aspx\" href=\"http:\/\/blogs.msdn.com\/b\/cesardelatorre\/archive\/2010\/12\/20\/handling-sql-azure-connections-issues-using-entity-framework-4-0.aspx\">http:\/\/blogs.msdn.com\/b\/cesardelatorre\/archive\/2010\/12\/20\/handling-sql-azure-connections-issues-using-entity-framework-4-0.aspx<\/a>&nbsp;<\/p>\n<p>Also, this link, provided by the AppFabric Customer Advisory Team, is quite useful, too:<\/p>\n<p><a title=\"http:\/\/blogs.msdn.com\/b\/appfabriccat\/archive\/2010\/12\/11\/sql-azure-and-entity-framework-connection-fault-handling.aspx\" href=\"http:\/\/blogs.msdn.com\/b\/appfabriccat\/archive\/2010\/12\/11\/sql-azure-and-entity-framework-connection-fault-handling.aspx\">http:\/\/blogs.msdn.com\/b\/appfabriccat\/archive\/2010\/12\/11\/sql-azure-and-entity-framework-connection-fault-handling.aspx<\/a><\/p>\n<p>But, this info was all related to EF 4.0, ObjectContext, EF Templates (STE &amp; POCO T4templates, etc.), not about EF 4.3, CODE FIRST APPROACH and DbContext.<\/p>\n<p>Therefore, this post is about how to handle <strong>SQL Azure Connection Faults<\/strong> (something common in the Windows Azure platform) when using EF 4.3, DbContext and CODE FIRST APPROACH.<\/p>\n<p><strong>THE ISSUE:<\/strong><\/p>\n<p>If you take a look to the link provided by the AppFabric Customer Advisory Team, about the &lsquo;<strong>Retry Policy<\/strong>&rsquo; when creating the EF Context. The tactic is to implement the <strong>OnContextCreated()<\/strong> partial method of your <strong>model context<\/strong> (based on <strong>ObjectContext<\/strong>) which is called each time a new context is instantiated. In this partial method, <strong>you employ a retry policy which opens a connection, submits a dummy query and handles exceptions with proper closure of invalid connections. In this way, the pool is &lsquo;cleansed&rsquo; of all connections that have disconnected due to network glitches or idle expirations.<\/strong><\/p>\n<p>This was the initial suggested code for <b>EF 4.0<\/b> and <b>ObjectContext<\/b>:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>partial void <span style=\"font-size: x-small\">OnContextCreated() <br \/><\/span><\/strong>{ <br \/>&nbsp;&nbsp;&nbsp; int MaxRetries = 10; <br \/>&nbsp;&nbsp;&nbsp; int DelayMS = 100;<\/p>\n<p>&nbsp;&nbsp;&nbsp; <strong>RetryPolicy policy = new RetryPolicy&lt;SqlAzureTransientErrorDetectionStrategy&gt;(MaxRetries, TimeSpan.FromMilliseconds(DelayMS)); <br \/>&nbsp;&nbsp;&nbsp; policy.ExecuteAction(() =&gt; <br \/><\/strong>&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string ss = Connection.ConnectionString; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><span style=\"font-size: x-small\">Connection.Open(); <br \/><\/span><\/strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var storeConnection = (SqlConnection)((EntityConnection)Connection).StoreConnection; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new SqlCommand(&#8220;declare @i int&#8221;, storeConnection).ExecuteNonQuery(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ throw new ApplicationException(&#8220;Test only&#8221;); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception e) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Connection.Close(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw e; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp; ); <br \/>}<\/p>\n<p>The issue is that this is based on the <strong>OnContextCreated()<\/strong> method which is related to your <strong>ObjectContext<\/strong> class.<\/p>\n<p>When using <strong>CODE-FIRST<\/strong> approach, we use a context based on the <strong>DbContext<\/strong>, so we&rsquo;ll have a slightly different code.<\/p>\n<p>Instead of placing that code into the OnContextCreated() we can put it within the constructor of your <strong>model context<\/strong> (in this case based on <strong>DbContext<\/strong>).<\/p>\n<p>Take into account that we&rsquo;re using the <strong>RetryPolicy<\/strong> class, which is implemented in a special building block made by <em>Microsoft Pattern &amp; Practices <\/em>team, called &lsquo;<strong>Transient Fault Handling Application Block<\/strong>&rsquo; (good job <strong>Grigori &amp; P&amp;P team<\/strong>!).<\/p>\n<p>It is available from <strong>NuGet<\/strong>: <a href=\"http:\/\/nuget.org\/packages\/EnterpriseLibrary.WindowsAzure.TransientFaultHandling\">http:\/\/nuget.org\/packages\/EnterpriseLibrary.WindowsAzure.TransientFaultHandling<\/a><\/p>\n<p>See also:<\/p>\n<p><a href=\"http:\/\/blogs.msdn.com\/b\/agile\/archive\/2011\/12\/02\/announcing-the-enterprise-library-integration-pack-for-windows-azure-with-autoscaling-transient-fault-handling-and-more.aspx\">http:\/\/blogs.msdn.com\/b\/agile\/archive\/2011\/12\/02\/announcing-the-enterprise-library-integration-pack-for-windows-azure-with-autoscaling-transient-fault-handling-and-more.aspx<\/a>&nbsp;<\/p>\n<p>Anyway, this would be the first version of the code for EF &amp; DbContext (Still, it doesn&rsquo;t work):<\/p>\n<p>using System.Data.Entity; <br \/>using System.Data.Entity.Infrastructure; <br \/>using System.Data.Entity.ModelConfiguration.Conventions;<\/p>\n<p>&hellip;<\/p>\n<p>using System.Data.Common; <br \/>using System.Data.SqlClient; <br \/>using System.Data.EntityClient; <br \/>using Microsoft.Practices.TransientFaultHandling; <br \/>using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure; <\/p>\n<p><strong>public class <span style=\"background-color: #ffff00\">MyBCUnitOfWork : DbContext<\/span> <br \/><\/strong>{ <br \/>&nbsp;&nbsp;&nbsp; \/\/Constructor where we handle &#8216;SQL Azure and Entity Framework Connection Fault Handling&#8217; <br \/><strong>&nbsp;&nbsp;&nbsp; <span style=\"background-color: #ffff00\">public MyBCUnitOfWork() <br \/><\/span><\/strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base() <br \/>&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>DbConnection<\/strong> currentDbConn = this.Database.Connection; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int MaxRetries = 10; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int DelayMS = 100;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>RetryPolicy policy = new RetryPolicy&lt;SqlAzureTransientErrorDetectionStrategy&gt;(MaxRetries, TimeSpan.FromMilliseconds(DelayMS)); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; policy.ExecuteAction(() =&gt;<\/strong> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>currentDbConn.Open(); <span style=\"background-color: #ffff00\"> \/\/(Issue here!!)<\/span><\/strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var storeConnection = (SqlConnection)currentDbConn; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new SqlCommand(&#8220;declare @i int&#8221;, storeConnection).ExecuteNonQuery(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ throw new ApplicationException(&#8220;Test only&#8221;); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception e) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; currentDbConn.Close(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw e; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );<\/p>\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n<p>But, that is not the only difference we need to make, because if you only put that code within the constructor, when you execute your app, you&rsquo;ll get an error when executing any EF code (your Linq sentences in any real DB access you do):<\/p>\n<p>Exception mesage: &ldquo;<strong><span style=\"font-size: x-small;background-color: #ffff00\">EntityConnection can only be constructed with a closed DbConnection<\/span><\/strong>&rdquo;<\/p>\n<p>This issue is due to some internal changes in EF 4.3 DbContext, and it is explained by my friend <strong>Diego Vega<\/strong> in the following post:<\/p>\n<h5>Exception from DbContext API: EntityConnection can only be constructed with a closed DbConnection<\/h5>\n<p><a href=\"http:\/\/blogs.msdn.com\/b\/diego\/archive\/2012\/01\/26\/exception-from-dbcontext-api-entityconnection-can-only-be-constructed-with-a-closed-dbconnection.aspx\">http:\/\/blogs.msdn.com\/b\/diego\/archive\/2012\/01\/26\/exception-from-dbcontext-api-entityconnection-can-only-be-constructed-with-a-closed-dbconnection.aspx<\/a><\/p>\n<p>It is a very nice explanatory information about that issue. In any case, the solution for that exception is to open the connection using the ObjectContext object instead of the DbConnection.<\/p>\n<p>Instead of this line:<\/p>\n<p><strong>currentDbConn.Open(); <span style=\"background-color: #ffff00\"> \/\/(Issue here!!)<\/span><\/strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>We use this other line of code:<\/p>\n<p><strong>((IObjectContextAdapter)this).ObjectContext.Connection.Open();<\/strong><\/p>\n<p>Then, we&rsquo;ll be able to run all the workaround within a DbContext constructor, like the final code below:<\/p>\n<p><strong>public class MyBCUnitOfWork <br \/>&nbsp;&nbsp;&nbsp; : DbContext, IQueryableUnitOfWork<\/strong> <br \/>{ <br \/>&nbsp;&nbsp;&nbsp; \/\/Constructor where we handle &#8216;SQL Azure and Entity Framework Connection Fault Handling&#8217; <br \/>&nbsp;&nbsp;&nbsp; <strong>public MyBCUnitOfWork() <br \/><\/strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base() <br \/>&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/SQL Azure and Entity Framework Connection Fault Handling <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/See: <a href=\"http:\/\/blogs.msdn.com\/b\/appfabriccat\/archive\/2010\/12\/11\/sql-azure-and-entity-framework-connection-fault-handling.aspx?Redirected=true\">http:\/\/blogs.msdn.com\/b\/appfabriccat\/archive\/2010\/12\/11\/sql-azure-and-entity-framework-connection-fault-handling.aspx?Redirected=true<\/a> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DbConnection currentDbConn = this.Database.Connection; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int MaxRetries = 10; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int DelayMS = 100;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RetryPolicy policy = new RetryPolicy&lt;SqlAzureTransientErrorDetectionStrategy&gt;(MaxRetries, TimeSpan.FromMilliseconds(DelayMS)); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; policy.ExecuteAction(() =&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/(CDLTLL) Using ObjectContext.Connection because there&#8217;s an issue when using DbConnection.Open() here. <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/See this blog post from Diego Vega: <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/<a href=\"http:\/\/blogs.msdn.com\/b\/diego\/archive\/2012\/01\/26\/exception-from-dbcontext-api-entityconnection-can-only-be-constructed-with-a-closed-dbconnection.aspx\">http:\/\/blogs.msdn.com\/b\/diego\/archive\/2012\/01\/26\/exception-from-dbcontext-api-entityconnection-can-only-be-constructed-with-a-closed-dbconnection.aspx<\/a> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><span style=\"background-color: #ffff00\">((IObjectContextAdapter)this).ObjectContext.Connection.Open();<\/span><\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var storeConnection = (SqlConnection)currentDbConn; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new SqlCommand(&#8220;declare @i int&#8221;, storeConnection).ExecuteNonQuery(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ throw new ApplicationException(&#8220;Test only&#8221;); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception e) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; currentDbConn.Close(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw e; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );<\/p>\n<p>&nbsp;&nbsp;&nbsp; } <\/p>\n<p>With that, we would have the &lsquo;<strong>Retry Policy<\/strong>&rsquo; implemented when creating any <strong>DbContext<\/strong> object, so you employ a <strong>retry policy which opens a connection, submits a dummy query and handles exceptions with proper closure of invalid connections. In this way, the connection-pool is &lsquo;cleansed&rsquo; of all connections that have disconnected due to network glitches, Sql Azure cluster-fail-over or idle expirations.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; I&rsquo;ve been working migrating an App to Windows Azure and SQL Azure. This app is using Entity Framework 4.3 Code First and DbContext. One important point is related to the SQL Azure Conection Fault Handling. If you don&rsquo;t know about this topic, you can read this explanatory info: http:\/\/social.technet.microsoft.com\/wiki\/contents\/articles\/1541.sql-azure-connection-management-en-us.aspx Then, regarding a workaround when [&hellip;]<\/p>\n","protected":false},"author":362,"featured_media":12806,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[48,90,114],"class_list":["post-303","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cesardelatorre","tag-entity-framework","tag-sql-azure","tag-window-azure"],"acf":[],"blog_post_summary":"<p>&nbsp; I&rsquo;ve been working migrating an App to Windows Azure and SQL Azure. This app is using Entity Framework 4.3 Code First and DbContext. One important point is related to the SQL Azure Conection Fault Handling. If you don&rsquo;t know about this topic, you can read this explanatory info: http:\/\/social.technet.microsoft.com\/wiki\/contents\/articles\/1541.sql-azure-connection-management-en-us.aspx Then, regarding a workaround when [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts\/303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/comments?post=303"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts\/303\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/media\/12806"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/media?parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/categories?post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/tags?post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}