原文链接: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. Cpu 主频与睿频

    主频就是一颗CPU的运行频率.比如一颗CPU是2.3G,无论是单核还是多核,所有的核心都是工作在2.3G. 睿频是Intel的一项加速技术,指当启动一个运行程序后,处理器会自动加速到合适的频率,而原来 ...

  2. curl 文件上传

    curl_file_create (带路径的文件名 [, 文件mimetype , 上传数据里的文件名] ) ; new cURLFile (带路径的文件名 [, 文件mimetype , 上传数据里 ...

  3. How to uninstall Maya on a Mac

    How to uninstall Maya on a Mac Products and versions covered  By:  Support Jun 15 2018 SHARE ADD TO ...

  4. android studio 代码模板

    作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com android studio 代码模板 an ...

  5. 10.23 正睿停课训练 Day7

    目录 2018.10.23 正睿停课训练 Day7 A 矩形(组合) B 翻转(思路) C 求和(思路 三元环计数) 考试代码 B1 B2 C 2018.10.23 正睿停课训练 Day7 期望得分: ...

  6. [模板][P3808]AC自动机(简单版)

    Description: 求n个模式串中有几个在文本串中出现 Solution: 模板,详见代码: #include<bits/stdc++.h> using namespace std; ...

  7. 2016年3月4日Android实习笔记

    1.让水平LinearLayout中的两个子元素分别居左和居右 在LinearLayout中有两个子元素,LinearLayout的orientation是horizontal.需要让第一个元素居左, ...

  8. putty失活不挂起运行

    https://blog.csdn.net/c1481118216/article/details/53010963 以下方式是试过了https://www.cnblogs.com/mysqlplus ...

  9. 15个优秀的开源项目,让你轻松应对Android开发

    一.huabanDemo-android 应用material design版的花瓣网App 项目地址 https://github.com/LiCola/huabanDemo 二.创建视差效果引导页 ...

  10. .net framework 4.6 asp.net mvc 使用NLog

    NUGET添加NLog和NLog.Config的引用 配置NLog.config <?xml version="1.0" encoding="utf-8" ...