实体框架/ MVC3:暂时禁用验证 [英] Entity Framework/MVC3: temporarily disable validation
问题描述
问题是:我可以临时禁用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屋!