久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

LINQ to SQL 更新中的(C# 變量的)默認值問題

Default Values (of C# variables) Issue in LINQ to SQL Update(LINQ to SQL 更新中的(C# 變量的)默認值問題)
本文介紹了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:

  1. Can LINQ-to-SQL omit unspecified columns on insert so a database default value is used?

  2. How can I bind an Enum to a DbType of bit or int?

  3. Linq to SQL: Why am I getting IDENTITY_INSERT errors?

  4. 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# 變量的)默認值問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 午夜亚洲| 婷婷综合色 | 草比av| 国产美女在线播放 | 亚洲精品区 | 小草久久久久久久久爱六 | 国产精品美女久久久久久久网站 | 黑人精品欧美一区二区蜜桃 | 欧美日韩综合 | 成人久久久久久久久 | 日韩一区二区三区在线看 | 国产一级片在线观看视频 | a毛片| 日日夜夜天天干 | www.99热.com | 蜜月aⅴ国产精品 | 最新国产精品精品视频 | www.日韩 | 中文字幕精品一区二区三区精品 | 久久久久九九九女人毛片 | 天天综合91| 午夜视频在线 | 日韩成人免费av | 成人免费视频网站在线观看 | 日本成人福利视频 | 精品久久精品 | 日韩在线视频一区二区三区 | 午夜电影网 | 日韩一区二区三区在线 | 日韩高清国产一区在线 | 国产美女黄色片 | 国产成人精品一区二区三区视频 | 成人无遮挡毛片免费看 | 国产乱码精品一区二区三区忘忧草 | 蜜臀久久99精品久久久久久宅男 | 国产成人精品一区二区三区在线 | 亚洲国产精品一区二区三区 | 久久久精品综合 | 欧美一级欧美三级在线观看 | 91文字幕巨乱亚洲香蕉 | 日日日日操 |