title: TryUpdateModelAsync Opens and closes connection 1000's times
body: <p>I have a section of code...</p>
  
  <pre><code>TryUpdateModelAsync(tn.IT_Task, "IT_Task");
  </code></pre>
  
  <p>...that used to work reasonably speedy. Now, that same piece of code is achingly slow. Sometimes up to 10-15 seconds to complete.</p>
  
  <p>Using...</p>
  
  <pre><code>DB.ITConsole.Database.Log = s =&gt; System.Diagnostics.Debug.WriteLine(s);
  </code></pre>
  
  <p>...to output what was happening in the background, I found that the method was opening and closing the database literally several 1000 times. Seemed to me to be at least as many times as there are "IT_Task" records. Perhaps more. Small sample of the output below...</p>
  
  <p>What's going on? How can I get a single record on the model updated from the views collection?</p>
  
  <pre><code>Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Project_Note_ID] AS [Project_Note_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Description] AS [Description]
      FROM [dbo].[IT_Project_Note] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "164" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Task_ID] AS [Task_ID], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Reported_By_ID] AS [Reported_By_ID], 
      [Extent1].[NAB_ID] AS [NAB_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Description] AS [Description], 
      [Extent1].[Task_Category_ID] AS [Task_Category_ID], 
      [Extent1].[Task_Type_ID] AS [Task_Type_ID], 
      [Extent1].[Task_Status_ID] AS [Task_Status_ID], 
      [Extent1].[Task_Priority_ID] AS [Task_Priority_ID], 
      [Extent1].[Task_Severity_ID] AS [Task_Severity_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Date_Due] AS [Date_Due]
      FROM [dbo].[IT_Task] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "164" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Project_Note_ID] AS [Project_Note_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Description] AS [Description]
      FROM [dbo].[IT_Project_Note] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "203" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Task_ID] AS [Task_ID], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Reported_By_ID] AS [Reported_By_ID], 
      [Extent1].[NAB_ID] AS [NAB_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Description] AS [Description], 
      [Extent1].[Task_Category_ID] AS [Task_Category_ID], 
      [Extent1].[Task_Type_ID] AS [Task_Type_ID], 
      [Extent1].[Task_Status_ID] AS [Task_Status_ID], 
      [Extent1].[Task_Priority_ID] AS [Task_Priority_ID], 
      [Extent1].[Task_Severity_ID] AS [Task_Severity_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Date_Due] AS [Date_Due]
      FROM [dbo].[IT_Task] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "203" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Task_Note_ID] AS [Task_Note_ID], 
      [Extent1].[Task_ID] AS [Task_ID], 
      [Extent1].[Description] AS [Description], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[CC_User] AS [CC_User]
      FROM [dbo].[IT_Task_Note] AS [Extent1]
      WHERE [Extent1].[Task_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "15669" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Project_Note_ID] AS [Project_Note_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Description] AS [Description]
      FROM [dbo].[IT_Project_Note] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "235" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Task_ID] AS [Task_ID], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Reported_By_ID] AS [Reported_By_ID], 
      [Extent1].[NAB_ID] AS [NAB_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Description] AS [Description], 
      [Extent1].[Task_Category_ID] AS [Task_Category_ID], 
      [Extent1].[Task_Type_ID] AS [Task_Type_ID], 
      [Extent1].[Task_Status_ID] AS [Task_Status_ID], 
      [Extent1].[Task_Priority_ID] AS [Task_Priority_ID], 
      [Extent1].[Task_Severity_ID] AS [Task_Severity_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Date_Due] AS [Date_Due]
      FROM [dbo].[IT_Task] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "235" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Project_Note_ID] AS [Project_Note_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Description] AS [Description]
      FROM [dbo].[IT_Project_Note] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "243" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Task_ID] AS [Task_ID], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Reported_By_ID] AS [Reported_By_ID], 
      [Extent1].[NAB_ID] AS [NAB_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Description] AS [Description], 
      [Extent1].[Task_Category_ID] AS [Task_Category_ID], 
      [Extent1].[Task_Type_ID] AS [Task_Type_ID], 
      [Extent1].[Task_Status_ID] AS [Task_Status_ID], 
      [Extent1].[Task_Priority_ID] AS [Task_Priority_ID], 
      [Extent1].[Task_Severity_ID] AS [Task_Severity_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Date_Due] AS [Date_Due]
      FROM [dbo].[IT_Task] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "243" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Project_Note_ID] AS [Project_Note_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Description] AS [Description]
      FROM [dbo].[IT_Project_Note] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "257" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Task_ID] AS [Task_ID], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Reported_By_ID] AS [Reported_By_ID], 
      [Extent1].[NAB_ID] AS [NAB_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Description] AS [Description], 
      [Extent1].[Task_Category_ID] AS [Task_Category_ID], 
      [Extent1].[Task_Type_ID] AS [Task_Type_ID], 
      [Extent1].[Task_Status_ID] AS [Task_Status_ID], 
      [Extent1].[Task_Priority_ID] AS [Task_Priority_ID], 
      [Extent1].[Task_Severity_ID] AS [Task_Severity_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Date_Due] AS [Date_Due]
      FROM [dbo].[IT_Task] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "257" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Project_Note_ID] AS [Project_Note_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Description] AS [Description]
      FROM [dbo].[IT_Project_Note] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "285" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  
  Opened connection at 15/06/2017 16:59:40 +01:00
  
  SELECT 
      [Extent1].[Task_ID] AS [Task_ID], 
      [Extent1].[Entered_By_ID] AS [Entered_By_ID], 
      [Extent1].[Reported_By_ID] AS [Reported_By_ID], 
      [Extent1].[NAB_ID] AS [NAB_ID], 
      [Extent1].[Project_ID] AS [Project_ID], 
      [Extent1].[Description] AS [Description], 
      [Extent1].[Task_Category_ID] AS [Task_Category_ID], 
      [Extent1].[Task_Type_ID] AS [Task_Type_ID], 
      [Extent1].[Task_Status_ID] AS [Task_Status_ID], 
      [Extent1].[Task_Priority_ID] AS [Task_Priority_ID], 
      [Extent1].[Task_Severity_ID] AS [Task_Severity_ID], 
      [Extent1].[Date_Entered] AS [Date_Entered], 
      [Extent1].[Date_Due] AS [Date_Due]
      FROM [dbo].[IT_Task] AS [Extent1]
      WHERE [Extent1].[Project_ID] = @EntityKeyValue1
  
  
  -- EntityKeyValue1: "285" (Type = Int32, IsNullable = false)
  
  -- Executing at 15/06/2017 16:59:40 +01:00
  
  -- Completed in 0 ms with result: SqlDataReader
  
  
  
  Closed connection at 15/06/2017 16:59:40 +01:00
  </code></pre>
  
  <p>UPDATE - What am I trying to do with the TryUpdateModelAsync?</p>
  
  <p>To my mind, my knowledge, TryUpdateModelAsync would only do one thing. Apologies if I am wrong. </p>
  
  <p>I have created a model based on a particular record in the database along with some supporting material for dropdowns etc on that page, then sent it to the page for editing during the GET phase. Then during the POST phase I create another (same) model based on the same record. </p>
  
  <p>The TryUpdateModelAsync is intended to take the data from the return POST and update the original record with that data, before I can then save it. </p>
  
  <p>I really wasn't expecting the process to look in the database at all to merge these two sets of data. I would have only expected access to the database to take place upon the SaveChanges.</p>