Code First DataAnnoatations


Entity Framework – Code First DataAnnoatations

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.

  • Key
  • Timestamp
  • ConcurrencyCheck
  • Required
  • MinLength
  • MaxLength
  • StringLength

System.ComponentModel.DataAnnotations.Schema namespace includes the following attributes that impact the schema of the database.

  • Table
  • Column
  • Index
  • ForeignKey
  • not mapped
  • InverseProperty

Key

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.

  • The convention is to look for a property named "Id" or one that combines the class name and "Id," such as "StudentId."
  • The property will map to a primary key column in the database.

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.

Timestamp

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.

  • It's more common to use row version or timestamp fields for concurrency checking.
  • Rather than using the ConcurrencyCheck annotation, you can use the more specific TimeStamp annotation as long as the property type is a byte array.
  • You can only have one timestamp property in a given class.

ConcurrencyCheck

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.

MaxLength

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.

MinLength

The MinLength attribute also specifies additional property validations, just as you did with MaxLength.

StringLength

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.

Table

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 −

  • You can use Code First with an existing database. But it's not always the case that the names of the classes match the names of the tables in your database.
  • Table attribute overrides this default convention.
  • Code First will create a table with a specified name in the Table attribute for a given domain class.

Column

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 −

  • Column attribute overrides the default convention.
  • EF Code First will create a column with a specified name in the Column attribute for a given property.

Index

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.

  • You can create an index on one or more columns using the IndexAttribute.
  • Adding the attribute to one or more properties will cause EF to create the corresponding index in the database when it creates the database.
  • dexes make data retrieval faster and more efficient in most cases. However, overloading a table or view with indexes could unpleasantly affect the performance of other operations such as inserts or updates.
  • Indexing is the new feature in Entity Framework where you can improve the performance of your Code First application by reducing the time required to query data from the database.
  • You can add indexes to your database using the Index attribute and override the default Unique and Clustered settings to get the index best suited to your scenario.
  • By default, the index will be named IX_

Foreign Key

Code First convention will take care of the most common relationships in your model, but there are some cases where it needs help.

not mapped

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

InverseProperty is used when you have multiple relationships between classes.

3