9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first.aspx
EF 6 Code-First系列文章目录:
- 1 翻译系列:什么是Code First(EF 6 Code First 系列)
- 2.翻译系列:为EF Code-First设置开发环境(EF 6 Code-First系列)
- 3.翻译系列:EF Code-First 示例(EF 6 Code-First系列)
- 4.翻译系列:EF 6 Code-First默认约定(EF 6 Code-First系列)
- 5.翻译系列:EF 6中数据库的初始化(EF 6 Code-First 系列)
- 6.翻译系列:EF 6 Code-First中数据库初始化策略(EF 6 Code-First系列
- 7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)
- 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
- 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
- 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
- 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
- 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】
- 9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】
- 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
- 10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】
- 10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】
- 11.翻译系列:在EF 6中配置一对零或者一对一的关系【EF 6 Code-First系列】
- 12.翻译系列:EF 6 中配置一对多的关系【EF 6 Code-First系列】
- 13.翻译系列:Code-First方式配置多对多关系【EF 6 Code-First系列】
- 14.翻译系列:从已经存在的数据库中生成上下文类和实体类【EF 6 Code-First系列】
- 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】
- 16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】
- 17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】
- 18.翻译系列:EF 6 Code-First 中的Seed Data(种子数据或原始测试数据)【EF 6 Code-First系列】
- 19.翻译系列:EF 6中定义自定义的约定【EF 6 Code-First约定】
- 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
- 20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】
- 20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】
- 21.翻译系列:Entity Framework 6 Power Tools【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.配置文件中配置连接字符串:
<connectionStrings>
<add name="Constr" connectionString="Server=.;Database=EFAnnotationTableDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/>
</connectionStrings>
6.测试程序:
class Program
{
static void Main(string[] args)
{
using (var db = new EFDbContext())
{
List<Student> lstStuModel= db.StudentTable.ToList();
}
Console.WriteLine("success");
Console.ReadKey();
}
}
运行程序:【出现success字样,说明已经生成数据库和数据表成功了!】

我们看一下数据库:

这就是EF默认为我们生成的数据表,可以看到,表名称默认是实体名称+s后缀。
现在我们使用数据注解:修改一下Student实体:

运行之前,我们需要先手动删除一下刚才生成的数据库和数据表。因为这里我没有启用数据库迁移技术。

可以看到生成的表名是:StudentInfo了。现在我们使用数据注解,指定一下表的模式名称:

算了,我还是修改一下代码:免得每次测试都要手动删除数据库。【PS:这里直接运行就会报下图错误:】

我们改一下:上下文类的代码,

然后运行:

成功了,我们看下数据库:

看到了么,模式名,变成了我们设定的My.好了,这一篇数据注解之Table,就介绍完了,大家有不明白的可以留言,我会一一回复,谢谢支持!
9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】的更多相关文章
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...
- 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx EF ...
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...
随机推荐
- 【莫比乌斯反演】HDU1695_GCD
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 第一道莫比乌斯反演 感觉很巧妙的就是利用了F(x)=(n/x)*(m/x) 之后的那个去重也挺不 ...
- 032 HDFS中高可用性HA的讲解
HDFS Using QJM HA使用的是分布式的日志管理方式 一:概述 1.背景 如果namenode出现问题,整个HDFS集群将不能使用. 是不是可以有两个namenode呢 一个为对外服务-&g ...
- 兼容ie10以下版本的placeholder属性
<script src="${ctx }/js/jquery.placeholder.js" type="text/javascript">< ...
- 算法初级面试题05——哈希函数/表、生成多个哈希函数、哈希扩容、利用哈希分流找出大文件的重复内容、设计RandomPool结构、布隆过滤器、一致性哈希、并查集、岛问题
今天主要讨论:哈希函数.哈希表.布隆过滤器.一致性哈希.并查集的介绍和应用. 题目一 认识哈希函数和哈希表 1.输入无限大 2.输出有限的S集合 3.输入什么就输出什么 4.会发生哈希碰撞 5.会均匀 ...
- JAXB注解 @XmlRootElement 及XML文件解析详解
@Retention(value=RUNTIME) @Target(value=TYPE) public @interface XmlRootElement @Inherited @Retention ...
- abstract class和interface有什么区别?
含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必 ...
- Alpha冲刺随笔三:第三天
课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(十天冲刺) 团队名称:葫芦娃队 作业目标:在十天冲刺里对每天的任务进行总结. 随笔汇总:https://www.cnblogs ...
- hibernate中多对一问题
例如User类中有个Group引用对应数据库就是groupId的外键了. 外键映射成一个类对吧. <many-to-one name="groupId" column=&qu ...
- python3基础之文件对象操作
1.向文本文件中写入内容 s = 'Hello world\n文本文件的读取方法\n文本文件的写入方法\n' # 需要写入文件的字符串 print('显示需要写入的内容:\n{0:s}'.format ...
- [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树
[BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树 题目大意: 给定一个\(n(n\le50)\)个点,\(m(m\le1000 ...