第十五节: EF的CodeFirst模式通过DataAnnotations修改默认协定

时间:2021-08-13 15:14:58

一. 简介

1. DataAnnotations说明:EF提供以特性的方式添加到 domain classes上,其中包括两类:

   A:System.ComponentModel.DataAnnotations命名空间下的特性是表中列的属性的。

    包括:Key、Required、MinLength和MaxLength、StringLength、Timestamp、ConcurrencyCheck。

  B:System.ComponentModel.DataAnnotations.Schema命名空间下的特性是控制数据库结构的。

    包括:Table、Column、ForeignKey、NotMapped。

2. 特性介绍

① Key :声明主键

② Required:非空声明

③ MinLength和MaxLength:设置string类型的最大长度和最小长度,数据库的对应nvarchar

④ StringLength:设置string类型的长度,数据库对应nvarchar

⑤ Timestamp:将byte[]类型设置为timestamp类型

⑥ ConcurrencyCheck:并发检查,执行update操作时,会检查并发性(乐观锁)

⑦ Table: 给代码中的类换一个名来映射数据库中的表名.(还可以设置表的架构名称  [Table("myAddress", Schema = "Admin")]   )

⑧ Column: 给代码中类的属性换一个名来映射数据库中表的列名. (还可以设置列的类型、列在表中的显示顺序    [Column("myAddressName2", Order = 1, TypeName = "varchar")])

⑨ ForeignKey:设置外键,特别注意里面的参数填写什么.

⑩ NotMapped: 类中的列名不在数据库表中映射生成. (还可以只设置get属性或者只设置set属性,在数据库中也不映射)

   另外还有:Index、InverseProperty、DatabaseGenerated、ComplexType 这四个都不常用,在这里就不多介绍了

  (详细可以看:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx)

二. 代码实战

  public class Student4
{
[Key] //主键声明
public string studentKey { get; set; } [Required] //非空声明
public string stuName { get; set; } [MaxLength()] //最大长度
public string stuTxt1 { get; set; } [MaxLength(), MinLength()] //最大长度和最小长度
public string stuTxt2 { get; set; } [Timestamp] //设置为时间戳
public byte[] rowVersion { get; set; } [ConcurrencyCheck] //并发检查
public string stuTxt3 { get; set; } public virtual StudentAddress4 stuAddress4 { get; set; }
} [Table("myAddress")] //设置类映射的数据库表名
//[Table("myAddress", Schema = "Admin")] //设置类映射的数据库表名和架构名
public class StudentAddress4
{
[ForeignKey("stu")] //设置外键(对应下面声明的 stu) //这里符合 类名+id(忽略大小写)的规则,所以自动生成主键
public string studentAddress4Id { get; set; } [Column("myAddressName")] //设置映射数据库中表的列名
public string stuAddressName { get; set; } [Column("myAddressName2", Order = , TypeName = "varchar")] //设置映射数据库中表的列名、顺序、类型
public string stuAddrssName2 { get; set; } [NotMapped]//不映射数据
public string addressNum { get; set; } //不映射数据
public string txt1 { get { return stuAddrssName2;} } //不映射数据
public string _txt2 = "";
public string txt2 { set { _txt2 = value; } } public virtual Student4 stu { get; set; } }
   public class dbContext4 : DbContext
{
public dbContext4()
: base("name=dbContext4")
{ }
public DbSet<Student4> Student4 { get; set; } public DbSet<StudentAddress4> StudentAddress4 { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
    <!--正宗的CodeFirst DataAnnotation-->
<add name="dbContext4" connectionString="data source=localhost;initial catalog=CodeFirstDB4;persist security info=True;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

第十五节: EF的CodeFirst模式通过DataAnnotations修改默认协定

三. 总结

  DataAnnotations通过注解形式改变数据库结构,非常灵活,当然EF还提供另外一种方式 Fluent API的形式,可以将一个类映射成多个数据库表,还可以将配置写成多个文件,方便控制。

   关于Fluent API更多用法,详解下一个章节。

!

  • 作       者 : Yaopengfei(姚鹏飞)
  • 博客地址 : http://www.cnblogs.com/yaopengfei/
  • 声     明1 : 本人才疏学浅,用郭德纲的话说“我是一个小学生”,如有错误,欢迎讨论,请勿谩骂^_^。
  • 声     明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。