Entity Framework Core

Entity Framework Core

What is Entity Framework Core?

Entity Framework Core (EF Core) is the latest version of the Entity Framework from Microsoft. It has been designed to be lightweight, extensible and support cross platform version of the popular Entity Framework data access technology.

One thing Entity Framework Core does is to serve as an object-relational mapper(ORM), which would help enable

  • .NET developers to work with a database using .NET objects. These objects are an instance of a particular class we create when working with .NET classes.

  • It also enables .NET developers to write most of the data access code that needs to be written in C#.

Entity Framework Core is a library that alllows developers to work abstractly with their database. It comes in two parts, the Object relational mapper(ORM) and the CLI tools. Entity Framework Core supports an array of database options, with the most popular being the SQL Server.

Features of Entity Framework Core

  1. Entity Framework Core can execute on Windows, Mac, Linux at the same time because Entity Framework is Cross Platform.

  2. Entity Framework Core utilizes LINQ Queries to retrieve the data from the database. This enables the execution of SQL queries directly from the database to do the query process.

  3. Entity Framework Core is used to create Entity Data Model(EDM) which is based on entities with the GET and SET properties of various data types. It is also used for all the database-related operations. It is a model that describes entities and the relationship between them.

Entity Framework Core offers to configure the Entity Framework Model using the data annotation attributes or API to override the default conventions. This data annotations are .NET attributes which can be applied on an entity class or property to override default conventions in EF 6 and EF core. This data annotation attributes are included in the System.ComponentModel.DataAnnotations and System.ComponentModel.DataAnnotations.Schema namespaces in EF Core as well EF 6. These attributes are not only used in Entity Framework Core but they can be also used in ASP.NET MVC or data contols. One good thing is that these data annotation attributes work in the same way in EF 6 and EF Core and are valid in both.

Some examples of attributes that can be found in System.Component.Model.DataA nnotations are:

  • KEY, which is applied to a property to specify a key property in an entity and makes the corresponding column the PrimaryKey column in the database.
using System.ComponentModel.DataAnnotations;
namespace App.Models{
    public class UserData{
        [Key]
        public int ID {get; set;} 
        }
}
  • REQUIRED, which can be applied to a property to specify that the corresponding column is a NotNull column in the database.
using System.ComponentModel.DataAnnotations;
namespace App.Models{
    public class UserData{
        [Required(ErrorMessage = "Email is require"]
        [EmailAddress(ErrorMessage = "Please enter a valid email address"]
        public string Email {get; set;} 
        }
}
  • MaxLength, which can be applied to a property to specify the maximum length of data value allowed for a property which in turn sets the size of a corresponding column in the database. It can be applied to the String or Byte properties of an entity.
using System.ComponentModel.DataAnnotations;
namespace App.Models{
    public class UserData{
        [MaxLength(50)]
        public string UserName {get; set;} 
        }
}
  • StringLength can also be applied to a property to specify the maximum string length allowed in the corresponding column in the database.

      using System.ComponentModel.DataAnnotations;
      namespace App.Models{
          public class UserData{
              [StringLength(50)]
              public string UserName {get; set;} 
              }
      }
    

    Also some attributes that can be found in System.ComponentModel.DataAnnotations.Schema are:

  • Table, which can be applied to an entity class to configure the corresponding table name and schema in the database.

      using System.ComponentModel.DataAnnotations.Schema;
      namespace App.Models{
          [Table("User")]
          public class UserData{
              public int UserID {get; set;}
              public string UserName {get; set;} 
              }
      }
    

    In the example above, Table is the attribute applied to the UserData entity class. So Entity Framework Core will override the default conventions and create the UserTable instead of the UserData table in the database.

  • Column also can be applied to a property to configure the corresponding column name, order and data type in the database.

       using System.ComponentModel.DataAnnotations.Schema;
       namespace App.Models{
    
          public class UserData{
              public int UserID {get; set;}
              [Column("Name")]
              public string UserName {get; set;} 
              }
      }
    

    In the example above, the column attribute is applied to UserName property. So Entity Framework Core will override the default conventions and create a Name column instead of the UserName column in the UserData table.

    Conclusion

    In all Entity Framework Core has helped developers easily create and also maintain the data-oriented application with less code. In terms of performance, in an Entity Framework Core, it is quite slow for the first request to fetch data but after that it is quick to fetch data from a database.