Default Values (of C# variables) Issue in LINQ to SQL Update(LINQ to SQL 更新中的(C# 变量的)默认值问题)
问题描述
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?
Note: I am using Attach method
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.
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Duration", DbType="Int NOT NULL"
public int Duration
Table DATA
Table Structure:
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
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<T>().Attach(entity);
Context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, entity);
}
}
Generated SQL
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
READING:
Can LINQ-to-SQL omit unspecified columns on insert so a database default value is used?
How can I bind an Enum to a DbType of bit or int?
Linq to SQL: Why am I getting IDENTITY_INSERT errors?
LINQ to SQL: Updating without Refresh when "UpdateCheck = Never"
I solved this issue by changing the update approach by following the answer in LINQ to SQL: Updating without Refresh when "UpdateCheck = Never"
UpdateCheck is set as Never for the Duration column
public void UpdateAccount()
{
//Used value from previous select
DateTime previousDateTime = new DateTime(2012, 6, 26, 11, 14, 15, 327);
int prevDuration = 0;
RepositoryLayer.Account accEntity = new RepositoryLayer.Account();
accEntity.AccountNumber = 1; //Primary Key
accEntity.ModifiedTime = previousDateTime; //Concurrency column
//accEntity.Duration = prevDuration;
accountRepository.UpdateChangesByAttach(accEntity);
//Values to be modified after Attach
accEntity.AccountType = "WIN-WIN";
accEntity.ModifiedTime = DateTime.Now;
try
{
accountRepository.SubmitChanges();
}
catch(System.Data.Linq.ChangeConflictException e)
{
throw new Exception(e.Message);
}
}
public virtual void UpdateChangesByAttach(T entity)
{
if (Context.GetTable<T>().GetOriginalEntityState(entity) == null)
{
//If it is not already attached
Context.GetTable<T>().Attach(entity);
}
}
Generated SQL
UPDATE [dbo].[Account]
SET [AccountType] = @p2, [ModifiedTime] = @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/26/2012 11:14:15 AM]
-- @p2: Input NChar (Size = 10; Prec = 0; Scale = 0) [WIN-WIN]
-- @p3: Input DateTime (Size = -1; Prec = 0; Scale = 0) [6/26/2012 11:16:29 AM]
这篇关于LINQ to SQL 更新中的(C# 变量的)默认值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:LINQ to SQL 更新中的(C# 变量的)默认值问题
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 使用 rss + c# 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01