DataAnnotations is used to configure the classes, highlighting the most commonly needed configurations. DataAnnotations are also understood by several .NET applications, such as ASP.NET MVC, which allows these applications to leverage the same annotations for client-side validations. DataAnnotation attributes override default CodeFirst conventions.
System.ComponentModel.DataAnnotations includes the following attributes that impact the nullability or size of the column.
System.ComponentModel.DataAnnotations.Schema namespace includes the following attributes that impact the schema of the database.
Entity Framework relies on every entity having a key value for tracking entities. One of the conventions that Code First depends on is how it implies which property is the key in each Code First class.
Entity Framework also supports composite keys. Composite keys are also primary keys that consist of more than one property. When you have composite keys, Entity Framework requires you to define the order of the key properties. You can do this using the Column annotation to specify an order.
Code First will treat Timestamp properties the same as ConcurrencyCheck properties, but it will also ensure that the database field that Code first generates is non-nullable.
The ConcurrencyCheck annotation allows you to flag one or more properties for concurrency checking in the database when a user edits or deletes an entity. If you've been working with the EF Designer, this aligns with setting a property's ConcurrencyMode to Fixed.
The MaxLength attribute allows you to specify additional property validations. It can be applied to a domain class's string or array type property. EF Code First will set the size of a column as specified in the MaxLength attribute.
The MinLength attribute also specifies additional property validations, just as you did with MaxLength.
StringLength also allows you to specify additional property validations like MaxLength. The only difference is that the StringLength attribute can only be applied to a string-type property of Domain classes.
Default Code First convention creates a table name similar to the class name. If you are letting Code First create the database and want to change the names of the tables it is making. Then −
It is also the same as the Table attribute, but the Table attribute overrides the table behavior while the Column attribute overrides the column behavior. Default Code First convention creates a column name similar to the property name. If you are letting Code First create the database and want to change the name of the columns in your tables. Then −
The Index attribute was introduced in Entity Framework 6.1. If you are using an earlier version, the information in this section does not apply.
Code First convention will take care of the most common relationships in your model, but there are some cases where it needs help.
By default conventions of Code First, every property of a supported data type that includes getters and setters is represented in the database. But this isn't always the case in your applications, and NotMapped attribute overrides this default convention. For example, you might have a property in the Student class such as FatherName, but it does not need to be stored.
InverseProperty is used when you have multiple relationships between classes.