可以为实体实现自定义验证,重写DBContext中的个ValidateEntity方法

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);
}
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);
}
}
}

EntityFramework 学习 一 Validate Entity的更多相关文章

  1. EntityFramework 学习 一 Colored Entity in Entity Framework 5.0

    You can change the color of an entity in the designer so that it would be easy to see related groups ...

  2. EntityFramework 学习 一 Update Entity Graph using DbContext:

    使用主键属性 每个实体必须有主键 默认值的id属性值必须为0 在context2中,它不知道实体的状态, 只能通过实体的主键来判断实体的状态 如果主键为0,则是新的对象,不为0 就是修改 Standa ...

  3. EntityFramework 学习 一 Add Entity Graph using DbContext:

    //Create student in disconnected mode Student newStudent = new Student() { StudentName = "New S ...

  4. EntityFramework 学习 一 Delete Entity using DBContext in Disconnected Scenario

    Student studentToDelete; . Get student from DB using (var ctx = new SchoolDBEntities()) { studentToD ...

  5. Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。

    <configSections> <!-- For more information on Entity Framework configuration, visit http:// ...

  6. Entity Framework Tutorial Basics(40):Validate Entity

    Validate Entity You can write custom server side validation for any entity. To accomplish this, over ...

  7. EntityFramework 学习 一 Change Tracking in Entity Framework

    EntityFramework自动跟踪上下文中已经加载的实体,DbChangeTracker类给你关于当前实体的所有跟踪信息 注意,每个实体都要有EntityKey(主键)的属性,EntityFram ...

  8. EntityFramework 学习 一 Entity Relationships 实体的关系

    下面,我们学习Entity Framework怎么管理实体间的关系 Entity Framework支持三种关系:一对一的关系.一对多的关系.多对多的关系 前面我们创建SchoolDB的实体数据模型, ...

  9. EntityFramework 学习 一 创建实体数据模型 Create Entity Data Model

    1.用vs2012创建控制台程序 2.设置项目的.net 版本 3.创建Ado.net实体数据模型 3.打开实体数据模型向导Entity Framework有四种模型选择 来自数据库的EF设计器(Da ...

随机推荐

  1. httpclient 怎么带上登录成功后返回的cookie值访问下一页面

    我是只很菜很菜的小鸟.刚上班,有这个一个需求.要我抓取别的网站的数据.     我根据用户密码登录一个网站成功后,生成一个cookie值.我已经获取到了.然后要带上这个cookie值进行下一页面的访问 ...

  2. Java 基础巩固,根深而叶茂

    #J2SE ##基础 八种基本数据类型的大小,以及他们的封装类. 八种基本数据类型,int ,double ,long ,float, short,byte,character,boolean 对应的 ...

  3. 【BZOJ2707】[SDOI2012]走迷宫 Tarjan+拓扑排序+高斯消元+期望

    [BZOJ2707][SDOI2012]走迷宫 Description Morenan被困在了一个迷宫里.迷宫可以视为N个点M条边的有向图,其中Morenan处于起点S,迷宫的终点设为T.可惜的是,M ...

  4. 分布式缓存的一致性Hash算法 2 32

    w 李智慧

  5. You can add an index on a column that can have NULL values if you are using the MyISAM, InnoDB, or MEMORY storage engine.

    w https://dev.mysql.com/doc/refman/5.7/en/create-index.html MySQL :: MySQL 5.7 Reference Manual :: B ...

  6. 2014 MapReduce

    function map(String name, String document): // name: document name // document: document contents fo ...

  7. 【转】NPIV - 连接虚拟机与存储的桥梁

    转自:http://blog.csdn.net/jewes/article/details/7705895 解决什么问题 我们知道在存储区域网络(SAN:storage area network),主 ...

  8. POCO c++ 使用例子

    .定时器 #include "Poco/Timer.h" #include "Poco/Thread.h" using Poco::Timer; using P ...

  9. python函数回顾:hex()

    描述 hex() 函数用于将10进制整数转换成16进制,以字符串形式表示. 语法 hex 语法: hex(x) 参数说明: x -- 10进制整数 返回值 返回16进制数,以字符串形式表示. 实例 & ...

  10. 前端基础-html(2)

    一.字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong>.<em>.<sup>.<sub&g ...