Databases Reference
In-Depth Information
NOTE
In dynamically generated web pages, the
StringLengthAttribute
is enforced by the
DynamicValidator
controls created by the field templates. Some field templates also
set the
MaxLength
property of the
TextBox
controls to limit the number of characters
users can enter.
One downside to using the
StringLengthAttribute
is that it duplicates information
about the size of the column. Normally, this information is stored in a single place in
your application—the Entity Data Model. When it is duplicated in a hand-written
StringLengthAttribute
, you need to remember to update it in addition to the EMDX if
the actual size of the database column changes. It would be a good idea to change the text
template to have this attribute automatically as discussed in the “Customizing Entity
Framework Code Generation” section earlier in this chapter.
RegularExpressionAttribute
The
RegularExpresionAttribute
can be used to ensure that string values match a particu-
lar pattern, such as a phone number or a URL. Here is how you can ensure that the
HomePage
property of the Supplier entity contains a valid URL:
[RegularExpression(
@”(?i)(http
|
https)://[a-z0-9\-\.]+\.(com
|
org
|
net
|
mil
|
edu)”)
]
public object HomePage { get; set; }
The
RegularExpressionAttribute
constructor takes a string that contains a regular expres-
sion pattern that the values are expected to match. In this example, the pattern expects
the URL to start with either
http
or
https
, followed by a “://,” followed by a sequence of
alpha-numeric symbols, dashes, and periods and one of five predefined top-level domains
(.com, .org, and so on).
NOTE
Detailed discussion of regular expressions would take another book of this size. Please
visit the following web page to learn more:
NOTE
In dynamically generated web pages, the
RegularExpressionAttribute
is typically
enforced by the
RegularExpressionValidator
controls created by the field
templates.