Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework/MVC3: temporarily disable validation

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?

like image 955
Alex Avrutin Avatar asked Nov 11 '11 21:11

Alex Avrutin


1 Answers

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

context.Configuration.ValidateOnSaveEnabled = false;
context.SaveChanges();
like image 66
Eranga Avatar answered Oct 12 '22 13:10

Eranga