原文地址: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. bzoj 4503

    没有权限号就只能对拍了 我们令?代表的T值=0,然后设出这样一个式子 这样一来,只要T和S在j位置匹配,当且仅当Dj=0,然后我们将这个式子拆开,变成下面那样 思路大概就是这样 最后发现答案应该是在a ...

  2. BZOJ1367 [Baltic2004]sequence 堆 左偏树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1367 题意概括 Description Input Output 一个整数R 题解 http:// ...

  3. 转载收藏用<meta name="ROBOTS"

    SEO优化meta标签 name="robots" content="index,follow,noodp,noydir"解释 (2012-10-11 10:33:08)转载   SEO优化meta标 ...

  4. String 方法indexOf()

    indexOf()来测是否包含子字符串. indexOf(sub, start) 如果return 是-1 包含没有找到字段.

  5. Unity3D 入门 游戏开发 Unity3D portal game development

    Unity3D 入门 游戏开发 Unity3D portal game development 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com ...

  6. AGC027 D - Modulo Matrix 构造

    目录 题目链接 题解 代码 题目链接 AGC027 D - Modulo Matrix 题解 从第左上角第一个点开始染色,相邻不同色,染法唯一 那么一个点的四周与他不同色,我们另这个点比四周都大,那么 ...

  7. BZOJ.3425.[POI2013]Polarization(DP 多重背包 二进制优化)

    BZOJ 洛谷 最小可到达点对数自然是把一条路径上的边不断反向,也就是黑白染色后都由黑点指向白点.这样答案就是\(n-1\). 最大可到达点对数,容易想到找一个点\(a\),然后将其子树分为两部分\( ...

  8. Python解释数学系列——分位数Quantile

    跳转到我的博客 1. 分位数计算案例与Python代码 案例1 Ex1: Given a data = [6, 47, 49, 15, 42, 41, 7, 39, 43, 40, 36],求Q1, ...

  9. 潭州课堂25班:Ph201805201 第八课:函数基础和函数参数 (课堂笔记)

    1, 函数定义 def fun(): print('测试函数') fun() #调用函数 return 运行函数返回值 def fun(): name = [1,3,4,5] return name[ ...

  10. 47. 全排列 II

    47. 全排列 II 题意 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2]输出:[ [1,1,2], [1,2,1], [2,1,1]] 解题思路 去重的全排列 ...