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
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);
}
}