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

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

Column特性,可以应用于实体的一个或者多个属性上面,用来配置数据库中数据表中列的列名、列的数据类型以及列的先后顺序。Column特性重写了默认的约定。按照EF 6和EF Core中的默认约定,将会创建和属性相同的列名称,并且数据表,列的顺序和实体中属性的顺序一致。

Column Attribute: [Column (string name, Properties:[Order = int],[TypeName = string])

name:表的数据列的名称

Order:列的顺序,从索引0开始【可选的】

TypeName:列的类型名称【可选的】

下面的例子,改变了一个列的名称:

在上面的例子中,我们在StudentName属性上应用Column特性,所以EF将会重写默认约定,为我们创建一个名称为Name的数据列,而不是StudentName数据列:

列的数据类型

可以使用Column特性中的TypeName参数,来指定列的数据类型,如下:

在上面的例子中,我们在DataOfBirth属性上,设置了TypeName参数值为DataTime2,然后就会为我们生成类型为DataTime2的列,而不是DateTime类型的列。

列的先后顺序

使用从索引0开始的Order参数,来设置数据库中列的顺序。按照默认约定,主键列会是第一个,然后其他的列的顺序,就是基于属性在实体中的先后顺序了。

注意:Order参数必须要应用在实体的所有属性上面,并且索引数不能有一样的,索引从0开始。

上面的例子,将会在数据库中,生成这样的顺序的列:

好了,理论介绍完了,我们何不动手实践一下?

1.创建一个名称为EFAnnotationColumn的控制台应用程序。

2.安装EF【Install-Package EntityFramework -Version 6.2.0】

3.创建一个Book类:

  1. public class Book
  2. {
  3. public int BookID { get; set; }
  4.  
  5. public string BookName { get; set; }
  6.  
  7. public string AuthorName { get; set; }
  8.  
  9. public DateTime PublishedDate { get; set; }
  10.  
  11. public decimal Price { get; set; }
  12. }

4.创建一个上下文类:BookContext

  1. public class BookContext:DbContext
  2. {
  3. public BookContext():base("name=Constr")
  4. {
  5. Database.SetInitializer<BookContext>(new DropCreateDatabaseAlways<BookContext>());
  6. }
  7. public DbSet<Book> Books { get; set; }
  8. }

5.SQL连接字符串:

  1. <connectionStrings>
  2. <add name="Constr" connectionString="Server=.;Database=EFAnnotationColumnDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/>
  3. </connectionStrings>

6.测试代码:

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. using (var db = new BookContext())
  6. {
  7. Book bookModel = new Book()
  8. {
  9. BookName = "西游记",
  10. AuthorName = "吴承恩",
  11. PublishedDate = DateTime.Now.AddYears(-),
  12. Price=109.99M
  13. };
  14. db.Entry(bookModel).State = System.Data.Entity.EntityState.Added;
  15. db.SaveChanges();
  16. }
  17. Console.WriteLine("添加数据成功");
  18. Console.ReadKey();
  19. }
  20. }

运行代码:

我们看看数据库:

可以看到默认约定,为我们生成了数据库以及数据表Books,列的顺序就和实体中属性先后顺序一样。

我们来修改一下Book实体:

然后运行项目:可以看到BookName字段名称,经过配置,生成了名称为BookTitle列。

我们再修改一下Book实体:

接着运行程序;看到PublishedDate列的数据类型成了我们配置的Date。

再修改一下Book实体:

  1. public class Book
  2. {
  3. [Column(Order =3)]
  4. public int BookID { get; set; }
  5.  
  6. [Column("BookTitle",Order =4)]
  7. public string BookName { get; set; }
  8.  
  9. [Column(Order =1)]
  10. public string AuthorName { get; set; }
  11.  
  12. [Column(TypeName ="Date",Order =0)]
  13. public DateTime PublishedDate { get; set; }
  14.  
  15. [Column(Order =2)]
  16. public decimal Price { get; set; }
  17. }

我们使用数据注解,配置列的顺序是:PublishedDate -->AuthorName-->Price-->BookID-->BookName.那么实际上是不是这样呢,我们运行项目:

可以看到,列的顺序就是按照我们配置的这样。

有个疑问,上面说到,配置列的顺序的时候,必须对所有的列,都应用于Column的Order参数。我们来看看一个反例:

修改Book实体:

  1. public class Book
  2. {
  3. [Column(Order =)]
  4. public int BookID { get; set; }
  5.  
  6. public string BookName { get; set; }
  7.  
  8. [Column(Order =)]
  9. public string AuthorName { get; set; }
  10.  
  11. [Column(TypeName ="Date",Order =)]
  12. public DateTime PublishedDate { get; set; }
  13.  
  14. public decimal Price { get; set; }
  15. }

上面我们移除了BookName属性和Price属性的Column特性,没有设置Order。我们运行看看:

可以看到,EF是按照有设置Order参数的顺序来的,第一个是索引为0 的PublishedDate,第二个是索引为1的AuthorName,第三个就是索引为3的BookID了,然后后面两个就是按照他们在实体中的顺序来的了。

我们再改一下,只对一个属性设置Order,看看:

public int BookID { get; set; }

public string BookName { get; set; }

public string AuthorName { get; set; }

public DateTime PublishedDate { get; set; }

[Column(Order = 3)]
public decimal Price { get; set; }

上面代码,只对Price属性设置了Order,索引为3,我们运行看看:【可以看到Price成为了第一个列,排在最前面,然后后面的列的顺序,就是按照属性在实体中的先后顺序了。】

好了,这篇文章就介绍完了,主要讲解了数据注解特性之---Column,值得注意的是,当配置列的顺序的时候,必须对所有的列都应用于Column特性中的Order参数标记,索引从0开始,切记!!!

9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】的更多相关文章

  1. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】

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

  2. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

  3. 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】

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

  4. 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...

  5. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...

  6. 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】

    原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...

  7. 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】

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

  8. 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx EF ...

  9. 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】

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

随机推荐

  1. Python 程序下载经办人照片

    进行图片下载,需要提前准备好下载图片的存放文件夹: python在与文件.目录打交道时,少不了os模块.os模块包含普遍的操作系统功能. os.path.exists(filepath)——检验指定的 ...

  2. CMake Error at cuda_compile_generated_warp.cu.o.cmake:264 (message)

    今天,我来给大家分享一下opencv安装时报的错.然后讲错是怎么解决的. 为啥老是写一些环境搭建的博客?因为环境搭建琐碎而繁杂,希望写下来,帮助大家.让大家少走弯路. 专注主业,专注算法的实现和优化. ...

  3. react-native-pushy 热更新

    教程来源于官网: 准备工作 添加热更新功能 发布应用 说明: 在往 pushy 发布了安装包之后,后续都是通过下面 2个命令来发布 热更新版本的,而不是再次发布安装包, 在使用热更新服务更新版本的时候 ...

  4. JavaScript学习-3——数组、函数、递归

    本章目录 -----------①数组 -----------②函数 -----------③递归 一.数组 弱类型:任何类型数据,且没有强度限制: 强类型:同一类型的数据存储的集合(内存中连续存储) ...

  5. tp5文件上传展示

    //接收文件 $file = request()->file('image'); //将文件移动到框架应用根目录/public/uploads/ 目录下 $info = $file->mo ...

  6. SpringBoot +Jpa+ Hibernate+Mysql工程

    1 使用工具workspace-sts 3.9.5.RELEASE (1)新建一个SpringBoot 项目,选择加载项目需要的的组件.DevTools,JPA,Web,Mysql. Finish.  ...

  7. 配置远程主机http服务器 打包资源

    <1> 搭建nginx  验证nginx是否启动成功 https://blog.csdn.net/wdsdsdsds/article/details/51179780 https://ww ...

  8. Session & Cookie 简介

    (一)简介 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通 ...

  9. redis、mysql、mongdb的比较

    特点: 1-1 MySQL:1. 使用c和c++编写,并使用了多种编译器进行测试,保证源代码的可移植性2. 支持多种操作系统3. 为多种编程语言提供可API4. 支持多线程,充分利用CPU资源优化的S ...

  10. 整合Spring框架和MyBatis框架

    ------------------------siwuxie095                                 整合 Spring 框架和 MyBatis 框架         ...