原文地址: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. IIS 之 通过 Web.config 修改文件上传大小限制设置方法

    在IIS 6.0中,不设置默认大小为4M,设置文件上传大小的方法,maxRequestLength(KB),executionTimeout(毫秒),配置如下节点: <system.web> ...

  2. Codeforces 490F Treeland Tour 树形dp

    Treeland Tour 离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快.. #include<bits/stdc++.h> #define LL long ...

  3. Here We Go(relians) Again HDU2722

    处理完输入就是很简单的一题  但是输入好难 勉强找到一种能看懂的... #include<iostream> #include<stdio.h> #include<str ...

  4. python界面Tkinter编程(tkMessageBox对话框使用)

    python界面Tkinter编程(tkMessageBox对话框使用)     转载 https://blog.csdn.net/m_buddy/article/details/80105154 1 ...

  5. P2393 yyy loves Maths II

    P2393 yyy loves Maths IIlong double比如保留5位小数*1000000都变成整数最后再/1000000避免精度误差scanf("%Lf",& ...

  6. docker 部署springboot应用

    第一步:搭建springboot的web应用,可在CMD命令行中通过mvn install命令将应用打成jar包:如demo-0.0.1-SNAPSHOT.jar 第二步:将jar包copy到cent ...

  7. CSS之文本溢出隐藏,不定宽高元素垂直水平居中、禁止页面文本复制

    1.如何让不固定元素宽高的元素垂直水平居中 .center { position: absolute; top: 50%; left: 50%; background-color: #000; wid ...

  8. Java中十个常见的违规编码

    摘要:作者Veera Sundar在清理代码工作时发现一些常见的违规编码,因此,Veera Sundar把针对常见的一些违规编码总结成一份列表,以便帮助Java爱好者提高代码的质量和可维护性. 最近, ...

  9. 生产环境中tomcat的配置

    生产环境中要以daemon方式运行tomcat 通常在开发环境中,我们使用$CATALINA_HOME/bin/startup.sh来启动tomcat, 使用$CATALINA_HOME/bin/sh ...

  10. [Agc005D]K Perm Counting

    [Agc005D] K Perm Counting Description 糟糕爷特别喜爱排列.他正在构造一个长度为N的排列.但是他特别讨厌正整数K.因此他认为一个排列很糟糕,当且仅当存在至少一个i( ...