原文链接: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. k8s 关键字以及管理流程。

    一.流程图如下 二.用户通过kubectl提交需要运行的docker container(pod). 三.api server把请求存储在etcd里面. 四.scheduler(调度)扫描,分配机器. ...

  2. win10下Spark的环境搭建

    win10下Spark的环境搭建 2018-08-19  18:36:45 一.jdk 1.8.0 安装与配置 二.scala 2.11.8 安装与配置http://www.scala-lang.or ...

  3. 010 处理模型数据(ModelAndView,Map Model,@SessionAttributes)

    1.处理数据模型 SpringMVC提供了几种途径出书模型数据 二:ModelAndView 1.介绍 2.index <%@ page language="java" co ...

  4. python的time模块总结

    python的time模块与random模块 目录 time模块 time模块 三种时间表示 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) : 通常来说,时间戳表示的是 ...

  5. 解决Ubuntu无法进行SSH连接的问题(以及如何使用SSH)

    我们在VM中安装好Ubuntu 虚拟机后,经常需要使用Xshell.ssh等工具进行远程连接,方便我们在两个操作系统中进行文件的复制与移动,但是有时候会出现无法连接的问题,原因可能是Ubuntu中默认 ...

  6. Python常用模块--logging

    (转载) 原文:http://www.cnblogs.com/dahu-daqing/p/7040764.html 1 logging模块简介 logging模块是Python内置的标准模块,主要用于 ...

  7. 不一样的go语言-构建系统与构件系统

    前言   代码的最后一步是构建成计算机可识别的二进制数据,然后才得以在计算机上运行.如果你曾经写过有点规模(至少数十个以上独立的源文件,且需要依赖第三方包)C语言项目,必定对C语言项目的构建过程印象深 ...

  8. SQL Server中查找包含某个文本的存储过程

    SELECT name,text from sysobjects o,syscomments s where o.id=s.id and text LIKE '%text%' and o.xtype= ...

  9. django-访问控制

    django自带的用户认证系统提供了访问控制的的功能.   1.只允许登录的用户登录   django的用户可分为两类,一是可认证的用户,也就是在django.contrib.auth.models. ...

  10. 系统windows版本修改

    系统基本信息修改 系统windows版本修改 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com ...