实体框架/MVC3:暂时禁用验证 [英] Entity Framework/MVC3: temporarily disable validation
问题描述
我首先使用 EF 代码.我大量使用代码注释来指定应如何验证数据(模型).但是,通常我只需要在 UI 层进行验证,并且能够在代码中将某些数据保存到数据库时取消验证.但是,一旦我指定了验证规则,它们就会应用于任何地方——在 UI、数据库、数据访问层上.
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.
我能否在 EF 层暂时禁用模型验证,以便我可以使用 SaveChanges()
保存数据而不会出现验证异常?
Can I temporarily disable model validation at EF layer so I can save the data using SaveChanges()
without getting validation exceptions?
推荐答案
您只需要在调用 SaveChanges()
之前在上下文类中设置 Configuration.ValidateOnSaveEnabled = false
.
You just need to set Configuration.ValidateOnSaveEnabled = false
in your context class before calling SaveChanges()
.
context.Configuration.ValidateOnSaveEnabled = false;
context.SaveChanges();
这篇关于实体框架/MVC3:暂时禁用验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!