原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-first.aspx

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

Required特性可以应用于一个实体的一个或多个属性上面。EF将会为标识Required特性的属性,在数据库表的列中生成不为空的列。

using System.ComponentModel.DataAnnotations;

public class Student
{
public int StudentID { get; set; }
[Required]
public string StudentName { get; set; }
}

上面代码中,Required特性,应用到了StudentName属性上,所以EF API将会在Students表中创建一个不为空的StudentName列:

现在,如果你保存数据的时候,没有填入StudentName属性的值,就会报System.Data.Entity.Validation.DbEntityValidationException异常:对于EF Core会报这个Microsoft.EntityFrameworkCore.DbUpdateException异常。
请注意:Required 同样可以用于ASP.NET MVC中,作为验证特性,了解更多在MVC中的实现方式,请看这个文章:Implement Validations in ASP.NET MVC

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

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

  8. 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...

  9. 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx EF ...

随机推荐

  1. mysql_union操作符

    MySQL  UNION操作符 union操作符合并两个或多个 SELECT 语句的结果集. union:去重 union all:不去重 UNION 结果集中的列名总是等于 UNION 中第一个 S ...

  2. Nightmare HDU1072

    非常标准的BFS 第一次写错了很多 1.到达4时设置为墙就好了  避免了死循环 2.不用开d数组   在结构体里面就行了 3.结构体初始化函数的写法: Node(int x=0,int y=0,int ...

  3. python dictionay(字典 )基本用法

    dict(dictionary)是一系列无序对象的集合,由键-值对构成,通过读取键来取得对应的值,具有可变,无序,异构,可嵌套的属性. dict初始化1.直接采用字典格式 2.利用dict(),[注] ...

  4. HDU 1348 Wall 【凸包】

    <题目链接> 题目大意: 给出二维坐标轴上 n 个点,这 n 个点构成了一个城堡,国王想建一堵墙,城墙与城堡之间的距离总不小于一个数 L ,求城墙的最小长度,答案四舍五入. 解题分析: 求 ...

  5. 洛谷 [P1024]一元三次方程求解【二分答案】

    题目链接:https://www.luogu.org/problemnew/show/P1024 题目描述 有形如:ax3+bx2+cx+d=0 这样的一个一元三次方程.给出该方程中各项的系数(a,b ...

  6. Python3 图片水平镜像实现

    # -*- coding: utf-8 -*- """ Created on Sun Feb 4 12:15:38 2018 @author: markli " ...

  7. 潭州课堂25班:Ph201805201 爬虫高级 第一课 pyspider框架 (课堂笔记)

    利用wheel安装 S1: pip install wheelS2: 进入www.lfd.uci.edu/~gohlke/pythonlibs/,Ctrl + F查找pycurl 这个包名是pycur ...

  8. swoole深入学习 4. process

    swoole-1.7.2增加了一个进程管理模块,用来替代PHP的pcntl扩展.pcntl是php新增的一个多进程扩展,用来实现多进程,但是有很多不完善的地方,swoole 就完善了这些地方,而且使得 ...

  9. Java表达式转型规则

    Java表达式转型规则由低到高转换: 1.所有的byte,short,char型的值将被提升为int型: 2.如果有一个操作数是long型,计算结果是long型: 3.如果有一个操作数是float型, ...

  10. EF6 简单增删改查示例代码

    示例一: private DbContext _dbContext; public DbContext CurrentContext { get { if (_dbContext == null) { ...