【译】第12节---数据注解-ConcurrencyCheck

原文:http://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-in-code-first.aspx

ConcurrencyCheck属性可以应用于域类的属性。 当EF执行表的update命令时,Code First会在“where”子句中使用列的值。 当你想要使用现有列进行并发检查时,可以使用ConcurrencyCheck属性,而不是用并发的单独时间戳列。

请看以下示例:

using System.ComponentModel.DataAnnotations;

public class Student
{
    public Student()
    { 
        
    }

    public int StudentId { get; set; }
     
    [ConcurrencyCheck]
    public string StudentName { get; set; }
} 

如上例所示,ConcurrencyCheck属性应用于Student类的现有StudentName属性。

因此,Code-First将在update命令中包含StudentName列,以检查乐观并发。

exec sp_executesql
N'UPDATE [dbo].[Students] SET [StudentName] = @0 WHERE (([StudentId] = @1) AND ([StudentName] = @2))',
N'@0 nvarchar(max) ,
@1 int,
@2 nvarchar(max)
',
@0=N'Steve',
@1=1,
@2=N'Bill' go

注意,TimeStamp属性只能应用于类中的单字节数组属性,而ConcurrencyCheck属性可以应用于具有任何数据类型的任意数量的属性。

 

有关乐观锁和悲观锁、示例等请参考园友文章:http://www.cnblogs.com/Gyoung/archive/2013/01/18/2866649.html

posted @ 2017-07-21 10:29  talentzemin  阅读(370)  评论(0编辑  收藏  举报