LINQ到SQL:更新无刷新的时候“UpdateCheck的=从不” [英] LINQ to SQL: Updating without Refresh when “UpdateCheck = Never”

查看:213
本文介绍了LINQ到SQL:更新无刷新的时候“UpdateCheck的=从不”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在UpdateCheck的=从不除了一个领域的所有领域的帐户实体。而ModifiedTime字段使用UpdateCheck的=始终。其目的是 - 并发检查应根据ModifiedTime仅列。

有关测试目的,我提供了ModifiedTime硬价值codeD在C#code。所以,没有必要提取任何值从数据库并发。仍是code将只更新,如果我呼吁刷新方法的数据库。这似乎很奇怪。任何方法来避免这种情况?

请参考:在 http://msdn.microsoft更新无需查询栏目.COM / EN-US /库/ bb386929.aspx

注:我想,以避免在SELECT语句中不使用刷新

生成的SQL

  SELECT [T0]。[账户号码],[T0]。[AccountType],[T0]。[时间],[T0]。[DepositedAmount],[T 0]。[ prefernce],[T0]。[注释],[T0]。[ModifiedTime]
 FROM [DBO]。[帐户] AS [T0]
 WHERE [T0]。[账户号码] = @ P0

 -  @ P0:输入INT(尺寸= -1; preC = 0;规模= 0)[1]
 - 背景:sqlProvider的(SQL2008)型号:AttributedMetaModel体形:4.0.30319.1
 

更新查询

 更新[DBO]。[帐户]
SET [AccountType] = @ P2,[时间] = @ P3
WHERE([账户号码] = @ P0)
与([ModifiedTime] = @ p​​1)为

 -  @ P0:输入INT(尺寸= -1; preC = 0;规模= 0)[1]
 -  @ P1:输入的日期时间(尺寸= -1; preC = 0;规模= 0)[2012年6月25日下午5点08分32秒]
 -  @ P2:输入NCHAR(大小= 10; preC = 0;规模= 0)[SUCESS]
 -  @ P3:输入int(尺寸= -1; $ P $件= 0;量表= 0)[2]
 - 背景:sqlProvider的(SQL2008)型号:AttributedMetaModel体形:4.0.30319.1
 

表结构

  CREATE TABLE [DBO]。[帐户](
[账户号码] [INT] NOT NULL,
[AccountType] [NCHAR](10)NOT NULL,
[时间] [INT] NOT NULL,
[DepositedAmount] [INT] NULL,
[prefernce] [INT] NULL,
[评论] [为nvarchar(50)NULL,
[ModifiedTime] [日期时间] NOT NULL,
 约束[PK_Account] PRIMARY KEY CLUSTERED
(
[账户号码] ASC
)WITH(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]
)ON [PRIMARY]
 

C#code

 公共虚拟无效UpdateChangesByAttach(T实体)
    {

        如果(Context.GetTable< T>()GetOriginalEntityState(实体)== NULL)
        {
            //如果它尚未附
            Context.GetTable< T>()附加(实体)。
            Context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues​​,实体);
        }

    }

    公共无效UpdateAccount()
    {
        //从previous使用的值选择
        日期时间previousDateTime =新的日期时间(2012,6,25,17,8,32,677);

        RepositoryLayer.Account accEntity =新RepositoryLayer.Account();
        accEntity.AccountNumber = 1;

        accEntity.AccountType =SUCESS;
        accEntity.ModifiedTime = previousDateTime;
        accEntity.Duration = 2;

        accountRepository.UpdateChangesByAttach(accEntity);
        accountRepository.SubmitChanges();

    }
 

表数据

阅读:

  1. <一个href="http://stackoverflow.com/questions/3301811/linq-to-sql-how-to-update-the-only-field-without-retrieving-whole-entity?lq=1">LINQ到SQL:如何在不更新检索领域唯一完整的实体

  2. <一个href="http://stackoverflow.com/questions/399379/update-without-first-selecting-data-in-linq-2-sql">Update没有在LINQ 2 SQL第一选择数据?

  3. 更新选择在LINQ到SQL

  4. 的LINQ to SQL更新多张行

  5. <一个href="http://stackoverflow.com/questions/11186813/default-values-of-c-sharp-variables-issue-in-linq-to-sql-update">Default价值观问题在LINQ to(C#的变量)的SQL更新


解决方案

感谢@sgmoore。的值要更新的附加方法后都设置。现在,这是工作。有什么还没有改善?

生成的SQL

 更新[DBO]。[帐户]
SET [AccountType] = @ P2,[时间] = @ P3,[ModifiedTime] = @ P4
WHERE([账户号码] = @ P0)
      与([ModifiedTime] = @ p​​1)为

 -  @ P0:输入INT(尺寸= -1; preC = 0;规模= 0)[1]
 -  @ P1:输入的日期时间(尺寸= -1; preC = 0;规模= 0)[2012年6月25日下午5点08分32秒]
 -  @ P2:输入NCHAR(大小= 10; preC = 0;规模= 0)[下]
 -  @ P3:输入INT(尺寸= -1; preC = 0;规模= 0)[4]
 -  @ P4:输入的日期时间(尺寸= -1; preC = 0;规模= 0)[2012年6月26日上午10时29分19秒]
 - 背景:sqlProvider的(SQL2008)型号:AttributedMetaModel体形:4.0.30319.1
 

code

 公共无效UpdateAccount()
    {
        //从previous使用的值选择
        日期时间previousDateTime =新的日期时间(2012,6,25,17,8,32,677);

        RepositoryLayer.Account accEntity =新RepositoryLayer.Account();
        accEntity.AccountNumber = 1; //首要的关键
        accEntity.ModifiedTime = previousDateTime; //并发列

        accountRepository.UpdateChangesByAttach(accEntity);

        //值后附加修改
        accEntity.AccountType =下一步;
        accEntity.ModifiedTime = DateTime.Now;
        accEntity.Duration = 4;

        accountRepository.SubmitChanges();

    }

    公共虚拟无效UpdateChangesByAttach(T实体)
    {

        如果(Context.GetTable&LT; T&GT;()GetOriginalEntityState(实体)== NULL)
        {
            //如果它尚未附
            Context.GetTable&LT; T&GT;()附加(实体)。
        }

    }
 

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.

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?

Refer: "Updating Without Querying" section in http://msdn.microsoft.com/en-us/library/bb386929.aspx

Note: I am trying to avoid the SELECT statement by not using Refresh

Generated SQL

 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

Update Query

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

Table Structure

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]

C# Code

    public virtual void UpdateChangesByAttach(T entity)
    {

        if (Context.GetTable<T>().GetOriginalEntityState(entity) == null)
        {
            //If it is not already attached
            Context.GetTable<T>().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();

    }

Table Data

READING:

  1. LINQ to SQL: how to update the only field without retrieving whole entity

  2. Update without first selecting data in LINQ 2 SQL?

  3. UPDATE SELECT in LINQ to SQL

  4. linq to sql update mulitple rows

  5. Default Values (of C# variables) Issue in LINQ to SQL Update


解决方案

Thanks to @sgmoore . The values to be updated are set after the Attach method. Now it is working. Is there anything yet to improve?

Generated SQL

UPDATE [dbo].[Account]
SET [AccountType] = @p2, [Duration] = @p3, [ModifiedTime] = @p4
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) [NEXT]
-- @p3: Input Int (Size = -1; Prec = 0; Scale = 0) [4]
-- @p4: Input DateTime (Size = -1; Prec = 0; Scale = 0) [6/26/2012 10:29:19 AM]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1

CODE

    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; //Primary Key
        accEntity.ModifiedTime = previousDateTime; //Concurrency column

        accountRepository.UpdateChangesByAttach(accEntity);

        //Values to be modified after Attach
        accEntity.AccountType = "NEXT";
        accEntity.ModifiedTime = DateTime.Now;
        accEntity.Duration = 4;

        accountRepository.SubmitChanges();

    }

    public virtual void UpdateChangesByAttach(T entity)
    {

        if (Context.GetTable<T>().GetOriginalEntityState(entity) == null)
        {
            //If it is not already attached
            Context.GetTable<T>().Attach(entity);
        }

    }

这篇关于LINQ到SQL:更新无刷新的时候“UpdateCheck的=从不”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆