LINQ to SQL: Updating without Refresh when “UpdateCheck = Never”

前端 未结 1 1511
清歌不尽
清歌不尽 2020-12-06 21:09

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 c

相关标签:
1条回答
  • 2020-12-06 22:00

    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);
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题