HTML and CSS Reference
In-Depth Information
Entity Framework Data Model and Data Annotations
The example page stores user information in a SQL server table named Users . An Entity Framework data
model is used to add user data in the Users table. Figure 5-26 shows the User model class.
Figure 5-26. User model class
To perform the server-side validations, you use data annotation attributes. Instead of decorating the
User model class with the data annotations, it's a recommended practice to use a metadata class. A
metadata class is a class that contains the data annotations for various properties of the main class. The
metadata class is then associated with the main data model class. The advantage of using a metadata class
is that it isolates validation-specific metadata in a separate class. This way, even if the User model class is
regenerated for some reason, the data annotations won't be lost. The metadata class is shown in Listing
5-26.
Listing 5-26. User Metadata Class
public class UserMetadata
{
[Required]
[StringLength(50,MinimumLength=3)]
[DisplayName("Display Name")]
public string DisplayName { get; set; }
[Required]
[StringLength(250, MinimumLength = 10)]
[DisplayName("Email Address")]
public string Email { get; set; }
[Required]
Search WWH ::




Custom Search