原文地址: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.配置文件中配置连接字符串:

 <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 系列】的更多相关文章

  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.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

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

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

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

  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. 【莫比乌斯反演】HDU1695_GCD

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 第一道莫比乌斯反演 感觉很巧妙的就是利用了F(x)=(n/x)*(m/x) 之后的那个去重也挺不 ...

  2. 032 HDFS中高可用性HA的讲解

    HDFS Using QJM HA使用的是分布式的日志管理方式 一:概述 1.背景 如果namenode出现问题,整个HDFS集群将不能使用. 是不是可以有两个namenode呢 一个为对外服务-&g ...

  3. 兼容ie10以下版本的placeholder属性

    <script src="${ctx }/js/jquery.placeholder.js" type="text/javascript">< ...

  4. 算法初级面试题05——哈希函数/表、生成多个哈希函数、哈希扩容、利用哈希分流找出大文件的重复内容、设计RandomPool结构、布隆过滤器、一致性哈希、并查集、岛问题

    今天主要讨论:哈希函数.哈希表.布隆过滤器.一致性哈希.并查集的介绍和应用. 题目一 认识哈希函数和哈希表 1.输入无限大 2.输出有限的S集合 3.输入什么就输出什么 4.会发生哈希碰撞 5.会均匀 ...

  5. JAXB注解 @XmlRootElement 及XML文件解析详解

    @Retention(value=RUNTIME) @Target(value=TYPE) public @interface XmlRootElement @Inherited @Retention ...

  6. abstract class和interface有什么区别?

    含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必 ...

  7. Alpha冲刺随笔三:第三天

    课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(十天冲刺) 团队名称:葫芦娃队 作业目标:在十天冲刺里对每天的任务进行总结. 随笔汇总:https://www.cnblogs ...

  8. hibernate中多对一问题

    例如User类中有个Group引用对应数据库就是groupId的外键了. 外键映射成一个类对吧. <many-to-one name="groupId" column=&qu ...

  9. python3基础之文件对象操作

    1.向文本文件中写入内容 s = 'Hello world\n文本文件的读取方法\n文本文件的写入方法\n' # 需要写入文件的字符串 print('显示需要写入的内容:\n{0:s}'.format ...

  10. [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树

    [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树 题目大意: 给定一个\(n(n\le50)\)个点,\(m(m\le1000 ...