Validate Entity

You can write custom server side validation for any entity. To accomplish this, override ValidateEntity method of DBContext as shown below.

protected override System.Data.Entity.Validation.DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, System.Collections.Generic.IDictionary<object, object> items)
{
if (entityEntry.Entity is Student)
{
if (entityEntry.CurrentValues.GetValue<string>("StudentName") == "")
{
var list = new List<System.Data.Entity.Validation.DbValidationError>();
list.Add(new System.Data.Entity.Validation.DbValidationError("StudentName", "StudentName is required")); return new System.Data.Entity.Validation.DbEntityValidationResult(entityEntry, list);
}
}
return base.ValidateEntity(entityEntry, items);
}

As you can see in the above code, we are validating the Student entity. If StudentName is blank, then we are adding DBValidationError into DBEntityValidationResult. So whenever you call DBContext.SaveChanges method and you try to save Student entity without StudentName, then it will throw DbEntityValidationException. For example:

try
{
using (var ctx = new SchoolDBEntities())
{
ctx.Students.Add(new Student() { StudentName = "" });
ctx.Standards.Add(new Standard() { StandardName = "" }); ctx.SaveChanges();
}
}
catch (DbEntityValidationException dbEx)
{
foreach (DbEntityValidationResult entityErr in dbEx.EntityValidationErrors)
{
foreach (DbValidationError error in entityErr.ValidationErrors)
{
Console.WriteLine("Error Property Name {0} : Error Message: {1}",
error.PropertyName, error.ErrorMessage);
}
}
}

Entity Framework Tutorial Basics(40):Validate Entity的更多相关文章

  1. Entity Framework Tutorial Basics(4):Setup Entity Framework Environment

    Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...

  2. Entity Framework Tutorial Basics(42):Colored Entity

    Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that ...

  3. Entity Framework Tutorial Basics(27):Update Entity Graph

    Update Entity Graph using DbContext: Updating an entity graph in disconnected scenario is a complex ...

  4. Entity Framework Tutorial Basics(5):Create Entity Data Model

    Create Entity Data Model: Here, we are going to create an Entity Data Model (EDM) for SchoolDB datab ...

  5. Entity Framework Tutorial Basics(26):Add Entity Graph

    Add Entity Graph using DbContext: Adding entity graph with all new entities is a simple task. We can ...

  6. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  7. Entity Framework Tutorial Basics(32):Enum Support

    Enum in Entity Framework: You can now have an Enum in Entity Framework 5.0 onwards. EF 5 should targ ...

  8. Entity Framework Tutorial Basics(31):Migration from EF 4.X

    Migration from Entity Framework 4.1/4.3 to Entity Framework 5.0/6.0 To migrate your existing Entity ...

  9. Entity Framework Tutorial Basics(20):Persistence in Entity Framework

    Persistence in Entity Framework There are two scenarios when persisting an entity using EntityFramew ...

随机推荐

  1. openfaas 架构介绍

     此为官方介绍   Overview of OpenFaaS Function Watchdog You can make any Docker image into a serverless fun ...

  2. (转)android平台下使用点九PNG技术

    “点九”是andriod平台的应用软件开发里的一种特殊的图片形式,文件扩展名为:.9.png 智能手机中有自动横屏的功能,同一幅界面会在随着手机(或平板电脑)中的方向传感器的参数不同而改变显示的方向, ...

  3. PostgreSQL备份

    备份与恢复 postgresql自带了两个备份工具: pg_dump:可备份一个指定的database pg_dumpall:可一次性备份所有database数据以及系统全局数据  使用pg_dump ...

  4. SourceTree使用介绍

    SourceTree比命令行更容易操作,能更直观看到发生了什么.但是没有哪一家git图形化软件能完成git的所有操作,封装后的使用也隐藏了git的一些细节,在图形化工具出现一些非常罕见的情况时,还是需 ...

  5. 使用FormData实现ajax文件异步上传

    1.传统的web开发文件上传一般是基于form表单的文件上传,同步的方式,用户体验差,可控性也差 2.异步上传的实现 有以下方式 2.1 借助浏览器插件 一般需要安装一些类似flash的插件  这种方 ...

  6. Linux下查询一个包是32位还是64位

    Linux下查询一个包是32位还是64位 [root@localhost ~]# rpm -qa  --queryformat %-{name}-%{version}-%{release}-%{arc ...

  7. java多线程实现礼花绽放的效果,

    总结:主要是那个红点点在上升的过程中要涂黑色,其实它不是一个点,是一个长条,而是被涂成黑色而隐藏了.还有这个睡眠时间,多线程 是你在面板上随便点,会出现随机的颜色圆圈,点哪里,哪里就可以出现圆 imp ...

  8. thinkphp实现多个子查询语句

    sql语句博大精深 理解好sql语句,就能用好thinkphp等框架中的数据库操作 原sql SELECT a.*,b.* from (SELECT a.id as opener_id,a.name, ...

  9. 微信小程序之巧妙的封装

    巧妙的封装 暴露一个访问地址xapp.config.js module.exports = { api_host: `https://a.squmo.com/yizu` } 继续引入,加暴露api.c ...

  10. 第2章 深入分析java I/O的工作机制(下)

    2.6 设计模式解析之适配器模式 2.6.1 适配器模式的结构 把一个类的接口变换成一客户端能接受的另一个接口. Target(目标接口): 要转换的期待的接口. Adaptee(源角色):需要适配的 ...