Entity Framework/MVC3: temporarily disable validation

帅比萌擦擦* 提交于 2019-11-27 20:16:58

问题


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.

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


回答1:


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

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


来源:https://stackoverflow.com/questions/8099949/entity-framework-mvc3-temporarily-disable-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!