设为首页 加入收藏

TOP

9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】(一)
2019-09-17 18:58:26 】 浏览:75
Tags:9.1 翻译 系列 数据 注解 特性 ----Table Code-First

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

 

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

 

 

Table特性可以应用于一个领域类上面,用来在数据库中生成相应名称的数据表。它重写了EF 6和 EF Code 中默认的约定,根据默认约定,EF 6和EF Core创建的表的名称是实体名称+s(或者es),并且创建的数据表的列名称和实体属性名称一样。

Table Attribute: [Table(string name, Properties:[Schema = string])

name:数据表的名称

Schema:数据库的模式名称【可选的】

在上面的例子中,Table特性应用于Student实体上。所以,EF将会重写默认的约定,并且创建名称为StudentMaster的数据表,而不是名称为Students的数据表,例如:

 

使用Schema 属性来指定数据表的模式名称:

EF将会创建StudentMaster表,并且指定表的模式名为Admin:

 

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

1.创建一个控制台应用程序,名称为:EFAnnotationTable

2.安装EF:【install-package entityframework -version 6.2.0】

 

 3. 创建一个Student类:

 public class Student { public int StudentID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } }

4.创建一个上下文类EFDbContext:【base中的name=后面名称要和SQL连接字符串名称一样。】

public class EFDbContext:DbContext { public EFDbContext() : base("name=Constr") { } public DbSet<Student> StudentTable { get; set; } }

5.配置文件中配置连接字符串:

 <
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇9.2 翻译系列:数据注解特性之---.. 下一篇CYQ.Data 支持分布式数据库(主从..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目