当前位置: 移动技术网 > IT编程>开发语言>.net > EF CodeFirst系列(4)--- 数据注释属性

EF CodeFirst系列(4)--- 数据注释属性

2018年09月21日  | 移动技术网IT编程  | 我要评论

七大常委个人财产信息,龙巡天下之无童镇全集,猴子捞月英文版

efcodefirst模式使用的是约定大于配置的编程模式,这种模式利用默认约定根据我们的领域模型建立概念模型。然后我们也可以通过配置领域类来覆盖默认约定。

覆盖默认约定主要用两种手段:

1.数据注释属性(data annotations attributes)

2.fluentapi

1.数据注释属性

我们可以给领域类或者类的属性添加数据注释属性来实现覆盖默认约定,其实在mvc webapi中也会经常用到数据注释属性。一个栗子

[table("studentinfo",schema ="myschool")]//数据库名为myschool.studentinfo
public class student
{
    public student() { }
        
    [key]//默认把id或者classnameid作为主键。通过[key]可以指定主键,如果是数字类型,默认自增
    public int sid { get; set; }

    [column("name",order=1,typename="ntext")]//必有项:列名为name ;非必有项:顺序为1,数据库中类型为ntext(order是从0开始的)
    [maxlength(20)]
    public string studentname { get; set; }

   [notmapped]//不映射到数据库中
    public int? age { get; set; }

  public string phone{get{return "13545678901";}}//getter,setter有一个不存在,就不会映射到数据库

[index("indexname",isclustered =true ,isunique =true)]//索引名为indexname,是聚集索引和唯一索引,直接使用[index]时索引名为ix_studentno,为非聚集索引   public string studentno{get;set;}
[timestamp]//在update操作时,包含在where子句中 public byte[] rowversion{get;set;}   public int stdid { get; set; }    [foreignkey("stdid")] public virtual standard standard { get; set; } }

 

一些常用的数据注释属性

key 数据库中对应列为主键
timestamp

数据库中对应列为timestamp类型,主要用于解决高并发问题

注:一个类只能用一次,且修饰的属性必须为byte[]类型

concurrencycheck 数据库中对应列进行乐观并发检测,主要用于解决高并发问题
required 属性不为空,数据中对应列
minlength/maxlength 属性和数据库中的最小/最大的string长度
stringlength 属性和数据库中的最小,最大

架构属性

table 用于实体,配置实体对应的数据库表名和表结构
column 用于属性,配置属性对应数据库列名,顺序和数据类型
index 用于属性,配置对应数据库的列为索引
foreignkey 用于属性,指定属性为一个外键
notmapped 用于实体/属性,不在数据库中生成映射
databasegernerated 用于属性,设置数据库对应列值的生成,如identity,computed或者none

2.一些补充

1.复合主键

ef6中通过key和column属性可以实现复合主键,一个栗子:

public class student
{
    [key]
    [column(order=1)]
    public int studentkey { get; set; }
     
    [key]
    [column(order=2)]
    public int admissionnum { get; set; }
     
    public string studentname { get; set; }
}

数据库如下:

 

注意:ef core中不支持通过[key]属性来设置复合主键,我们可以通过fluent api的haskey()方法来实现

2.foreignkey的三种形式

一个栗子:

public class student
{
    public int studentid { get; set; }
    public string studentname { get; set; }
        
    //foreign key for standard
    public int standardid { get; set; }
    public standard standard { get; set; }
}

public class standard
{
    public int standardid { get; set; }
    public string standardname { get; set; }
    
    public icollection<student> students { get; set; }
}

在上边的栗子中,没有使用foreignkey覆盖默认的约定,那么安照默认约定执行:ef找到导航属性standard,找到standard的主键为standardid,正好student类中有standardid属性,那么就把standardid设为外键;如果student类没有standardid呢?ef会创建一个外键<导航属性名>_<导航属性主键>,在本例中就是standard_standardid

我们不想使用ef自动创建的外键,而是想把一个属性设置成外键,但是这个属性和导航属性的主键名不一致,我们应该怎么去实现呢?

1.[foreignkey(导航属性名)]   依赖实体(student依赖standard,所以student是依赖实体)中在我们想设置为外键的属性上指定导航属性

public class student
{
    public int studentid { get; set; }
    public string studentname { get; set; }
        
    [foreignkey("standard")]
    public int standardrefid { get; set; }
    public standard standard { get; set; }
}

public class standard
{
    public int standardid { get; set; }
    public string standardname { get; set; }
    
    public icollection<student> students { get; set; }
}

2.[foreignkey(导航属性名)] 依赖实体中在导航属性上指定属性名

public class student
{
    public int studentid { get; set; }
    public string studentname { get; set; }
        
    public int standardrefid { get; set; }
    
    [foreignkey("standardrefid")]
    public standard standard { get; set; }
}

public class standard
{
    public int standardid { get; set; }
    public string standardname { get; set; }
    
    public icollection<student> students { get; set; }
}

3.主实体中在导航属性上指定属性名(不推荐)

ublic class student
{
    public int studentid { get; set; }
    public string studentname { get; set; }
        
    public int standardrefid { get; set; }
    public standard standard { get; set; }
}

public class standard
{
    public int standardid { get; set; }
    public string standardname { get; set; }
    
    [foreignkey("standardrefid")]
    public icollection<student> students { get; set; }
}

3.concurrencycheck解决高并发

  concurrencycheck属性可以用在领域类的一个或多个属性中,领域类的属性使用它修饰后,数据库中对应的列会启用乐观并发检测。

一个栗子:

public class student
{
    public int studentid { get; set; }
     
    [concurrencycheck]
    public string studentname { get; set; }
}

上边栗子中studentname使用concurrencycheck注释,ef会在update时将studentname添加到where子句中。

using(var context = new schoolcontext()) 
{
    var std = new student()
    {
        studentname = "bill"
    };

    context.students.add(std);
    context.savechanges();

    std.studentname = "steve";
    context.savechanges();
}

在执行savechanges()方法时,执行的sql命令如下:

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用于任何数据类型的属性且能在一个类中使用多次。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网