• Skip to content
  • Skip to primary sidebar
  • Home
  • Angular
  • ASP.NET Core
  • Entity Framework
    • Entity Framework 6
    • Entity Framework Core
  • Crystal Reports
  • C#
  • ASP.NET
  • About Us
    • Privacy Policy
    • Contact Us

TekTutorialsHub

Free Online Tutorials

Data Annotations Entity Framework & EF Core

January 14, 2016 by TekTutorialsHub Leave a Comment

Code First Conventions (EF6)
Table Attribute
EF Core Conventions
 

In this tutorial, we will give you a short introduction to the data annotations in Entity Framework & Entity Framework Core (EF Core).  The Data annotations attribute classes are used to decorate the classes or properties. These attributes help Entity Framework Code First  & EF Core to apply pre-defined rules on entity models

We have seen how to use Entity Framework code first conventions or Entity Framework Core Convention to configure our models in our previous tutorials. The conventions are good but are limited in their functionalities. .EF uses the Data Annotations to further fine-tune the model. These attributes override the conventions.

In this article

  • What is Data Annotation Attributes
  • Data modelling attributes
  • Validation Related Attributes
  • Conclusion

What is Data Annotation Attributes

The Data Annotations are the attributes that are applied to the class or on the properties of the class. They provide additional meta data about the class or its properties. These attributes are not specific to Entity Framework or Entity Framework Core. They are part of larger .NET  /.NET Core Framework. These attributes are used by ASP.NET MVC or ASP.NET MVC Core Applications also.

The Data annotation attributes are grouped into two categories depending functionality provided by them

  1. Data modelling attributes
  2. Data Validation Attributes

Data modelling attributes

The Data modelling attributes specify the schema of the database. These attributes are present in the namespace  System.ComponentModel.DataAnnotations.Schema.

The following attributes are present in the above namespace

AttributeDescription
Table AttributeYou can specify the name of the table to which the entity class maps to using these attributes
Column AttributeAllows us to specify the name of the column.
ComplexType AttributeThis attribute specifies that the class is a complex type.
DatabaseGenerated AttributeThis attribute added on the properties whose value is automatically updated by the Database.
ForeignKey AttributeForeign Key Attribute is applied to a property, which participates as a foreign key in a relationship
InverseProperty AttributeInverse Property is used when you have many to many relationships between entities. Specified on the property which is at the other end of the relationship
NotMapped AttributeThe NotMapped attribute is applied to those properties, which you do not want to include in your database table.
IndexThis attribute placed on a property to indicate that this property should have a index in the database

Validation Related Attributes

The Validation related attributes reside in the  System.ComponentModel.DataAnnotations namespace. These attributes are used to enforce validation rules for the entity properties.

ATTRIBUTEDESCRIPTION
ConcurrencyCheck AttributeThis attribute is applied to a property, which participates in concurrency check validation while updating or deleting an entity
Key AttributeThis attribute is applied to the property or properties, that are part of the primary key
MaxLength AttributeThis validation attribute specifies the max length of the column in the database
MinLength AttributeThis validation attribute specifies the minimum length of the data allowed in a string or array property.
Required AttributeSpecifies that a data field value is required. Specify the column as non-nullable
StringLength AttributeSpecifies the minimum and maximum length of characters that are allowed in a data field. This attribute is similar to MaxLength & MinLength attribute
Timestamp AttributeSpecifies the data type of the column as a row version.

Conclusion

Entity Framework allows us various ways to configure the model class. Using Conventions,  using data annotation attributes or by using fluent API. In this tutorial, we looked at Data annotation attributes. In the next few tutorials, we will look at each of the above data annotations attributes and how to use them with examples.

Code First Conventions (EF6)
Table Attribute
EF Core Conventions
 

Filed Under: Entity Framework

Leave a Reply

wpdiscuz_captcharefresh
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
wpdiscuz_captcharefresh
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of

Primary Sidebar

Copyright ©2008-2018

About us Contact Privacy Policy

Feb,22,2019 04:40:01 AM

Copyright © 2019 · Magazine Pro on Genesis Framework · WordPress · Log in

wpDiscuz
Our web site uses cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more