Programming Entity Framework - Cdn.oreilly.com
Programming Entity Framework - Cdn.oreilly.com
Programming Entity Framework - Cdn.oreilly.com
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
When T4 generates the new classes, if it determines that the MaxLength is needed, it<br />
will write out a setter that includes the MaxLength validation; otherwise, the original<br />
setter will be called. You’ll also need to make a small change a few lines lower, to ensure<br />
that the field required by the validation is created—an if statement that already tests for<br />
generateAutomaticProperty also must test hasMaxLengthAttrib.<br />
Figure 13-5 shows the relevant section of the template after the changes from Example<br />
13-14 have been made as well as the change to check the value of<br />
hasMaxLengthAttrib.<br />
Figure 13-5. Placement of template modifications for MaxLength<br />
validation<br />
Once you have the new code in place, the validation will automatically be part of your<br />
generated class. Example 13-15 shows the addressed and Street1 properties of the<br />
Address class using the modified template. The addressed property was not<br />
impacted because it does not have a MaxLength attribute, but the Street1 property<br />
now has validation code using the MaxLength value, 50, found in the metadata.<br />
Example 13-15. The validation for Address.Street1 as generated from the modified<br />
template<br />
public int addressID<br />
{