Note that there are some explanatory texts on larger screens.

plurals
  1. POEF 5 Database First using custom validation
    text
    copied!<p>In order to experience the new features of Entity Framework I created a new MVC 4 internet application.<br/> I connected it to an existing database and generated the Model classes with the dbContext generator.</p> <p>By running the application I got some validation errors while editing a form. As example for a DateTime field the system was complaining if the date was inserted as "12/10/2012" instead of 2012-10-12 (as in the SQ Server notation). <br/>I tried to find the validation code in the project, but I could not find it anywhere in the generated code.</p> <p>One of my model classes is the following:</p> <pre><code>public partial class Artist { public Artist() { } public int Id { get; set; } public string Name { get; set; } public Nullable&lt;System.DateTime&gt; DateOfBirth { get; set; } public virtual Countries Countries { get; set; } } </code></pre> <p>How can I customize the validation errors if using the Database first approach? <br/>If I decor the models with my validation attributes, then they would be erased once the model classes are generated again.</p> <p>Moreover in a "real world" project where the use of an existing database is mandatory, what would be the best approach with the development of the model classes? <br/>Extending the classes automatically generated by adding partial classes with the same name?</p> <p>EDIT (Introduced part of the View):</p> <pre><code>@using (Html.BeginForm()) { @Html.ValidationSummary(false) &lt;fieldset&gt; &lt;legend&gt;Movie&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Name) @Html.ValidationMessageFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.DateOfBirth) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.DateOfBirth) @Html.ValidationMessageFor(model =&gt; model.DateOfBirth) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } </code></pre>
 

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