Note that there are some explanatory texts on larger screens.

plurals
  1. PODefault Values (of C# variables) Issue in LINQ to SQL Update
    primarykey
    data
    text
    <p>I have the following code for updating Account table with LINQ to SQL. AccountNumber is the primary key column. The only value which need to be updated is AccountType; however the Duration also gets updated with zero (default value for int). How can we avoid this unnecessary overwrite?</p> <p>Note: I am using <strong>Attach</strong> method</p> <p>Note: I understand the reason for this behavior. "The DataContext cannot distinguish between a field with an assigned value of zero and one that is merely unassigned.". I am looking for a solution to overcome this.</p> <pre><code>[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Duration", DbType="Int NOT NULL" public int Duration </code></pre> <p>Table DATA</p> <p><img src="https://i.stack.imgur.com/TpQS7.jpg" alt="enter image description here"></p> <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, 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>CODE</p> <pre><code> public void UpdateAccount() { RepositoryLayer.Account acc1 = new RepositoryLayer.Account(); acc1.AccountNumber = 4; acc1.AccountType = "Verify"; accountRepository.UpdateChangesByAttachAndRefresh(acc1); accountRepository.SubmitChanges(); } public virtual void UpdateChangesByAttachAndRefresh(T entity) { //Can GetOriginalEntityState cause any bug? Is it unnecessary? if (GetTable().GetOriginalEntityState(entity) == null) { //If it is not already attached Context.GetTable&lt;T&gt;().Attach(entity); Context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, entity); } } </code></pre> <p>Generated SQL</p> <pre><code>UPDATE [dbo].[Account] SET [AccountType] = @p3, [Duration] = @p4 WHERE ([AccountNumber] = @p0) AND ([AccountType] = @p1) AND ([Duration] = @p2) AND ([DepositedAmount] IS NULL) -- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [4] -- @p1: Input NChar (Size = 10; Prec = 0; Scale = 0) [TEST ] -- @p2: Input Int (Size = -1; Prec = 0; Scale = 0) [10] -- @p3: Input NChar (Size = 10; Prec = 0; Scale = 0) [Verify] -- @p4: Input Int (Size = -1; Prec = 0; Scale = 0) [0] -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1 </code></pre> <p>READING:</p> <ol> <li><p><a href="https://stackoverflow.com/questions/2555206/can-linq-to-sql-omit-unspecified-columns-on-insert-so-a-database-default-value-i">Can LINQ-to-SQL omit unspecified columns on insert so a database default value is used?</a></p></li> <li><p><a href="https://stackoverflow.com/questions/2686036/how-can-i-bind-an-enum-to-a-dbtype-of-bit-or-int">How can I bind an Enum to a DbType of bit or int?</a></p></li> <li><p><a href="https://stackoverflow.com/questions/637121/linq-to-sql-why-am-i-getting-identity-insert-errors">Linq to SQL: Why am I getting IDENTITY_INSERT errors?</a></p></li> <li><p><a href="https://stackoverflow.com/questions/11189688/linq-to-sql-updating-without-refresh-when-updatecheck-never?lq=1">LINQ to SQL: Updating without Refresh when “UpdateCheck = Never”</a></p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload