Overriding Code-First Conventions

ASP .Net Data annotations

'Data Annotations' is a way of adding validations to our class/models. Consider the below sample model:

class

{

    int employeeNumber;

    string name;

Here we don't have any validation. In other words, the name can be null (because Entity Framework considers string can have null values) and any length (supported by string); similarly, the employeeNumber can of any length.

Data annotations solve this problem. Additionally, we also have another approach: Fluent API (will be discussed in another post).

If I have to enforce the below model to have a name as a mandatory attribute and also to set the maximum and minimum length of the name attribute, we can do as below:

class

{

    int employeeNumber;

[Required]

[MaxLength(24) , MinLength(5)]

    string name;

More details on Data Annotations

Let us start with the demo:

please download the source code from here - 

 For more information on the code, please check this link - 


a.'Key' attribute

Consider below class:

 public class Author

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public IList<Subject> Subjects { get; set; }

    }

Entity Framework follows the contention of automatically assume a property as a key if property is named as id or combination of ClassName+'id' (like in above class AuthourId). But, if the property is something like AuthId, then it throws an exception asking to declare a key. In this case, we use [Key] attribute.

public class Author

    {

        [Key]

        public int AuthId { get; set; }

        public string Name { get; set; }

        public IList<Subject> Subjects { get; set; }

    }

We can also define a composite key; a composite key consists of two or more primary keys. If we need to mark a Name + AuthId as a composite key, then do it as below:

public class Author

    {

        [Key]

        public int AuthId { get; set; }

        [Key]

        public string Name { get; set; }

        public IList<Subject> Subjects { get; set; }

    }

b. 'Required' attribute.

Lets again consider the same class:

 public class Author

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public IList<Subject> Subjects { get; set; }

    }

Entity Framework has designed the table as below:


The Name column can have null values. If we need to enforce the name to be mandatory field, then we need to use Required attribute.

 public class Author

    {

        public int Id { get; set; }

        [Required]

        public string Name { get; set; }

        public IList<Subject> Subjects { get; set; }

    }

c. MinLength and MaxLength

Again, Let's again consider the same class:

 public class Author

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public IList<Subject> Subjects { get; set; }

    }

The property Name as no restriction on number of characters: it can be one or 100 characters. We can restrict the length of the characters by using MinLength and MaxLength attributes.


 public class Author

    {

        public int Id { get; set; }

        [MaxLength(24) , MinLength(5)]

        public string Name { get; set; }

        public IList<Subject> Subjects { get; set; }

    }

The Name attribute can have a minimum length of 5 and a maximum length of 24.

d. Table

By default, Entity Framework names the table name as the class name. If we need to change the table name to something else, use the Table attribute.

[Table("AuthorInfo")]

 public class Author

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public IList<Subject> Subjects { get; set; }

    }

c. Column

Very similar to the Table attribute, we do have a column name. As anybody can guess, it is used to change the name of the default column name in the DB.

 public class Author

    {

        public int Id { get; set; }

        [Column("AuthourName")]

        public string Name { get; set; }

        public IList<Subject> Subjects { get; set; }

    }