9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】

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

EF 6 Code-First系列文章目录:

在EF 6和EF Core中,ConcurrencyCheck可以应用于实体的一个或者多个属性上面。当属性被标识了这个特性,在生成的数据表中的相应的列,就会用来做乐观检查.

using System.ComponentModel.DataAnnotations;

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

在上面的例子中,ConcurrencyCheck特性应用在Student实体的StudentName属性上,所以EF将会在更新的时候,包含StudentName列,用于乐观检查,看看下面的例子:

using(var context = new SchoolContext()) 
{
    var std = new Student()
    {
        StudentName = "Bill"
    };

    context.Students.Add(std);
    context.SaveChanges();

    std.StudentName = "Steve";
    context.SaveChanges();
}

上面的代码例子,将会在更新的时候,生成这样的语句【Where条件中】:

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特性只能用在单独的byte数组属性上,而ConcurrencyCheck特性可以用在任何数据类型的的任何数量的属性上面。

转载于:https://www.cnblogs.com/caofangsheng/p/10681801.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值