title: LINQ to SQL: Updating without Refresh when “UpdateCheck = Never”
body: <p>I have an Account entity which has all fields in “UpdateCheck = Never” except one field. The “ModifiedTime” field uses “UpdateCheck=Always”. The intention is – concurrency check should be based on “ModifiedTime” column only. </p>
  
  <p>For the test purpose I am supplying the “ModifiedTime” value hard coded in C# code. So there is no need to fetch any value from database for concurrency. Still the code will update the database only if I call for the Refresh method. This seems strange. Any approach to avoid this?</p>
  
  <p>Refer: "Updating Without Querying" section in <a href="http://msdn.microsoft.com/en-us/library/bb386929.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/bb386929.aspx</a></p>
  
  <p>Note: I am trying to avoid the SELECT statement by not using Refresh</p>
  
  <p>Generated SQL</p>
  
  <pre><code> SELECT [t0].[AccountNumber], [t0].[AccountType], [t0].[Duration], [t0].[DepositedAmount], [t0].[Prefernce], [t0].[Comment], [t0].[ModifiedTime]
   FROM [dbo].[Account] AS [t0]
   WHERE [t0].[AccountNumber] = @p0
  
  -- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
  -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1
  </code></pre>
  
  <p>Update Query</p>
  
  <pre><code>UPDATE [dbo].[Account]
  SET [AccountType] = @p2, [Duration] = @p3
  WHERE ([AccountNumber] = @p0) 
  AND ([ModifiedTime] = @p1)
  
  -- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
  -- @p1: Input DateTime (Size = -1; Prec = 0; Scale = 0) [6/25/2012 5:08:32 PM]
  -- @p2: Input NChar (Size = 10; Prec = 0; Scale = 0) [SUCESS]
  -- @p3: Input Int (Size = -1; Prec = 0; Scale = 0) [2]
  -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1
  </code></pre>
  
  <p>Table Structure</p>
  
  <pre><code>CREATE TABLE [dbo].[Account](
  [AccountNumber] [int] NOT NULL,
  [AccountType] [nchar](10) NOT NULL,
  [Duration] [int] NOT NULL,
  [DepositedAmount] [int] NULL,
  [Prefernce] [int] NULL,
  [Comment] [nvarchar](50) NULL,
  [ModifiedTime] [datetime] NOT NULL,
   CONSTRAINT [PK_Account] PRIMARY KEY CLUSTERED 
  (
  [AccountNumber] ASC
  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,   ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  ) ON [PRIMARY]
  </code></pre>
  
  <p>C# Code</p>
  
  <pre><code>    public virtual void UpdateChangesByAttach(T entity)
      {
  
          if (Context.GetTable&lt;T&gt;().GetOriginalEntityState(entity) == null)
          {
              //If it is not already attached
              Context.GetTable&lt;T&gt;().Attach(entity);
              Context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, entity);
          }
  
      }
  
      public void UpdateAccount()
      {
          //Used value from previous select
          DateTime previousDateTime = new DateTime(2012, 6, 25, 17, 8, 32, 677);
  
          RepositoryLayer.Account accEntity = new RepositoryLayer.Account();
          accEntity.AccountNumber = 1;
  
          accEntity.AccountType = "SUCESS";
          accEntity.ModifiedTime = previousDateTime;
          accEntity.Duration = 2;
  
          accountRepository.UpdateChangesByAttach(accEntity);
          accountRepository.SubmitChanges();
  
      }
  </code></pre>
  
  <p>Table Data</p>
  
  <p><img src="https://i.stack.imgur.com/RZV1K.png" alt="enter image description here"></p>
  
  <p>READING:</p>
  
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/3301811/linq-to-sql-how-to-update-the-only-field-without-retrieving-whole-entity?lq=1">LINQ to SQL: how to update the only field without retrieving whole entity</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/399379/update-without-first-selecting-data-in-linq-2-sql">Update without first selecting data in LINQ 2 SQL?</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/1996911/update-select-in-linq-to-sql">UPDATE SELECT in LINQ to SQL</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/9385041/linq-to-sql-update-mulitple-rows">linq to sql update mulitple rows</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/11186813/default-values-of-c-sharp-variables-issue-in-linq-to-sql-update">Default Values (of C# variables) Issue in LINQ to SQL Update</a></p></li>
  </ol>
  
  <hr>