久久久久久久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 的方法調用)
主站蜘蛛池模板: 羞羞涩涩在线观看 | 九九导航| 亚洲欧美日韩电影 | 国产日韩中文字幕 | 久久国产精品99久久久久久丝袜 | 国产精品国产三级国产aⅴ中文 | 国产电影一区二区 | 国产精品国产 | 国产一区二区精品在线观看 | 午夜视频在线免费观看 | 日韩高清中文字幕 | 一区二区三区av | 精品一区av| 亚洲成人精品 | 国产亚洲精品精品国产亚洲综合 | 99爱视频 | 一区二区三区日本 | 国产精品久久久久久久久久软件 | 欧美在线视频一区 | 亚洲免费人成在线视频观看 | 日韩国产精品一区二区三区 | 综合久 | 精品综合视频 | 欧美精品1区 | 精品九九九 | 欧美九九九 | 国产视频二区 | 欧美日韩一区不卡 | 亚洲精品一区中文字幕乱码 | 91精品亚洲 | 成人一区二区在线 | 欧洲精品在线观看 | 日韩欧美在线一区 | 久久国产成人 | 国产精品www | 国产一区二区三区四区在线观看 | 精品国产黄a∨片高清在线 www.一级片 国产欧美日韩综合精品一区二区 | 日韩视频在线观看 | 成人妇女免费播放久久久 | 天堂资源 | 日韩欧美在线不卡 |