Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You were both right, I was under false assumption.</p> <p><strong>[Required] is not an equivalent of .IsRequired()</strong></p> <p>There are several possible solutions:</p> <p>1) <strong>The quick &amp; easy</strong>: <a href="http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model" rel="nofollow">Attributes</a> </p> <blockquote> <p><strong>Model Configuration overrides and Validation</strong></p> <p>With CodeFirst it is possible to override the configuration of the model defined with validation attributes, e.g. in the OnModelCreating method. Reconfiguring model affects validation since validation should use the actual model configuration – blindly use of attributes would cause validation errors for values that could be valid according to the overrides made in OnModelCreating(). Here are the three special cases for overrides made in OnModelCreating:</p> <ul> <li><p>If a property was decorated with [Required] attribute and was reconfigured as optional (.IsOptional() method) the [Required] attribute will be removed and as a result ignored when validation happens</p></li> <li><p>If a property was decorated with [StringLength] or [MaxLength] attribute and then was configured with new length (.HasMaxLength() method) new maximum length will be used if possible</p></li> <li><p>If a property was decorated with [StringLength] or [MaxLength] attribute and then was defined to be allowed maximum length (.IsMaxLength) then the attribute will be removed (if possible) and the length of the property value will not be checked</p></li> </ul> <p>Note that the above changes will be effective only if a property was decorated with some validation attributes. So, setting property as required (.IsRequired()) will not cause the property be validated against null value.<br> See <a href="http://blogs.msdn.com/b/adonet/archive/2010/12/15/ef-feature-ctp5-validation.aspx" rel="nofollow">EF Feature CTP5: Validation</a></p> </blockquote> <p>2) <strong>A quick fix</strong> but it's a dirty one as Mystere Man suggested<br> <a href="http://thedatafarm.com/blog/data-access/capturing-code-first-fluent-api-validationresults-to-display-in-mvc3-views/" rel="nofollow">http://thedatafarm.com/blog/data-access/capturing-code-first-fluent-api-validationresults-to-display-in-mvc3-views/</a></p> <p><a href="http://bradwilson.typepad.com/blog/2010/10/service-location-pt6-model-validation.html" rel="nofollow">http://bradwilson.typepad.com/blog/2010/10/service-location-pt6-model-validation.html</a></p> <p>3) <strong>The heavy one</strong>: <a href="http://msdn.microsoft.com/en-us/library/ff664356%28v=pandp.50%29.aspx" rel="nofollow">The Validation Application Block in Enterprise Library</a><br> <a href="http://bradwilson.typepad.com/blog/2009/10/enterprise-library-validation-example-for-aspnet-mvc-2.html" rel="nofollow">http://bradwilson.typepad.com/blog/2009/10/enterprise-library-validation-example-for-aspnet-mvc-2.html</a> but Enterprise Library is very often considered as an overkill</p> <p>4) <strong>Validation nirvana</strong>: <a href="http://fluentvalidation.codeplex.com/" rel="nofollow">FluentValidation</a> looks very promising and I'll definitely give it a try.<br> <a href="http://www.nuget.org/packages/FluentValidation.MVC3" rel="nofollow">http://www.nuget.org/packages/FluentValidation.MVC3</a><br> Cons: Not the best approach in an n-layer app. It's primarily focused on Views.</p> <p>5) <strong>N-Layer</strong><br> <a href="http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs" rel="nofollow">http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</a><br> This approach leads to problems with a validation on the client side so it must be solved separately.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload