实体框架/ MVC3:暂时禁用验证 [英] Entity Framework/MVC3: temporarily disable validation

查看:96
本文介绍了实体框架/ MVC3:暂时禁用验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF Code First。我大量使用代码注释来指定如何验证数据(模型)。然而,通常我只需要在UI层进行验证,并且能够在代码中将某些数据保存到数据库时抑制验证。但是,一旦指定了验证规则,它们就会在数据访问层上随处可见 - 在UI上。



问题是:我可以临时禁用EF层的模型验证,以便我可以使用 SaveChanges()没有得到验证异常?



谢谢你,祝一个愉快的一天!

解决方案

你只需要在你的调用 SaveChanges()之前的上下文类。

  context.Configuration.ValidateOnSaveEnabled = false; 
context.SaveChanges();


I am using EF Code First. I heavily use code annotations to specify how the data (model) should be validated. However, often I just need validation at UI layer and be able to suppress validation when I save certain data to the database in the code. However, once I specified validation rules, they are applied everywhere -- on the UI, on the database, on the data access layer.

The question is: can I temporarily disable model validation at EF layer so I can save the data using SaveChanges() without getting validation exceptions?

Thank you and have a good day!

解决方案

You just need to set Configuration.ValidateOnSaveEnabled = false in your context class before calling SaveChanges().

context.Configuration.ValidateOnSaveEnabled = false;
context.SaveChanges();

这篇关于实体框架/ MVC3:暂时禁用验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆