[Entity Framework] Code First – Data Annotation Classes

The attribute classes are in the “System.ComponentModel.DataAnnotations” namespace. These classes can be used for EF model classes as well as MVC model classes. It provides the validation services for target objects.

 

1. System.ComponentModel.DataAnnotations Namespace

The “System.ComponentModel.DataAnnotations namespace provides the following attribute classes that are used to define metadata for ASP.NET MVC and Dynamic Data controls.  Some classes are available from .NET 4.5.

  • KeyAttribute : denotes one or more properties that uniquely identify an entity
  • MaxLengthAttribute : specifies the maximum length of array or string data (.NET 4.5)
  • MinLengthAttribute : specifies the minimum length of array or string data (.NET 4.5)
  • RequiredAttribute : specifies that a data field value is required
  • TimestampAttribute : specifies the data type of the column as a row version
  • ConcurrencyCheckAttribute : specifies that a property participates in optimistic concurrency checks

In .NET 4.5, some attribute classes are defined in “System.ComponentModel.DataAnnotations.Schema” namespace.

  • ColumnAttribute : represents a column attribute
  • ComplexTypeAttribute : denotes that the class is a complex type
  • DatabaseGeneratedAttribute : represents a database generated attribute
  • ForeignKeyAttribute : denotes a property used as a foreign key in a relationship
  • InversePropertyAttribute : represents an inverse property attribute
  • NotMappedAttribute : denotes that a property or class should be excluded from database mapping
  • TableAttribute : specifies the database table that a class is mapped to

 

2. ValidationAttribute

The “System.ComponentModel.DataAnnotations.ValidationAttribute” is an abstract class for all validation related attribute classes (“RequiredAttribute“, “DataTypeAttribute“, “StringLengthAttribute“, etc…)

It has the following properties:

public string ErrorMessage { get; set; }

 

3. RequiredAttribute

The “RequiredAttribute” class specifies that a data field value is required. It can be applied to properties and fields.

public class RequiredAttribute : ValidationAttribute
{
  public RequiredAttribute();
  public bool AllowEmptyStrings { get; set; }  // indicates whether an empty string is allowed
}

 

4. MaxLengthAttribute / MinLengthAttribute

The “MaxLengthAttribute” and “MinLengthAttribute” classes specify the upper and lower limits of string or array data allowed in a property.

MaxLengthAttribute” affects the database but “MinLengthAttribute” is for validation only.

public class MaxLengthAttribute : ValidationAttribute
{
  public MaxLengthAttribute();  // maximum allowable length supported by the database
  public MaxLengthAttribute(int length);
  public int Length { get; private set; }
}
public class MinLengthAttribute : ValidationAttribute
{
  public MinLengthAttribute(int length);
  public int Length { get; private set; }
}

 

5. ColumnAttribute

ColumnAttribute” represents a column in database.

public class ColumnAttribute : Attribute
{
  public ColumnAttribute();
  public ColumnAttribute(string name); // name of the column

  public string Name { get; }  // column name
  public int Order { get; set; } // the order of the column
  public string TypeName { get; set; } // column type
 }

 

6. DatabaseGeneratedAttribute

The “DatabaseGeneratedAttribute” class represents a database generated attribute.

public class DatabaseGeneratedAttribute : Attribute
{
  public DatabaseGeneratedAttribute(DatabaseGeneratedOption databaseGeneratedOption);
  public DatabaseGeneratedOption DatabaseGeneratedOption { get; private set; }
}

public enum DatabaseGeneratedOption
{
  None,
  Identity,
  Computed
}

 

7. TimestampAttribute

The “TimestampAttribute” class specifies the data type of the column as a row version.

public sealed class TimestampAttribute : Attribute
{
  public TimestampAttribute();
}

 

8. ConcurrencyCheckAttribute

The “ConcurrencyCheckAttribute” specifies that a property participates in optimistic concurrency checks.

public sealed class ConcurrencyCheckAttribute : Attribute
{
  public ConcurrencyCheckAttribute();
}

 

9. ComplexTypeAttribute

The “ComplexTypeAttribute” denotes that the class is a complex type.

public class ComplexTypeAttribute : Attribute
{
  public ComplexTypeAttribute();
}

 

10. ForeignKeyAttribute

The “ForeignKeyAttribute” denotes a property used as a foreign key in a relationship.

public class ForeignKeyAttribute : Attribute
{
  public ForeignKeyAttribute(string name);
  public string Name { get; }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s