Updated March 29, 2023
Introduction to Entity Framework Core
Entity Framework (EF) Core is an Open-Source extensible version that is used popularly for data access technology and it is the latest version of EF after the EF 6.x. The Entity Framework Core which serves as an Object-Relational Mapper is an improved version of ADO.NET which gives developers an automated mechanism for retrieving and storing the data in the database. The Entity Framework EF is used with the .NET Core applications.
Overview Entity Framework Core
In early version mostly developers used to write the entire codes through ADO.NET which used to store and retrieve the data from the database. In ADO.NET we need to open the connection to the database and we need to create the dataset to store and retrieve the data to the database and it converts the DataSet to .NET Objects and so on. The whole process is a tedious thing to complete and also an error-prone process. Another option method Microsoft provides is the framework called EF which automates/ computerize the activities of database for the application.
Entity Framework (EF) Core is an Open-Source ORM-based Framework for .NET applications and it is an extensible version that is used popularly for data access technology. By using this Framework developers concentrate on data using objects of domain classes without approaching desired database tables. When using with EF Core mostly developers concentrate at only high level of concepts when dealing with data to build and continue data-oriented applications with only less code which compared to traditional applications.
In the above diagram, EF is suited between the database and Business Entity, it stores the data in properties of business entities and gets the data from the database, and transfers the business entity objects repeatedly.
Entity Framework Features
- Entity Framework helps to reduce the coding level which helps developers to build any applications easily, let’s see a few points below
- EF can execute on Windows, Mac, Linux at the same time (simultaneously) because Entity Framework is a Cross Platform.
- Entity Framework (EF) maintains LINQ Queries which are used to retrieve the data from the database. The Entity Framework enables to execution of the SQL Queries which directly from the database to do the query process.
- The EF used to create Entity Data Model (EDM) which is based on entities with the Get and Set properties of various data types. The Entity is used by Framework at the storing and querying of data to the database.
- EF mostly built INSERT, DELETE, UPDATE commands to the database based on the changes which occurred to our entities when calling the SaveChanges() method. EF offers the asynchronous SaveChangesAsync() method.
- EF mostly does transaction management when storing and querying the data. It offers the preference to customize the transaction management.
- EF offers the first level of cache out of the box, instead of that, the repeated query returns the data from the cache as an alternative to hitting the database.
- EF offers to configure the Entity Framework Model using the data annotation attributes or API to override the default conventions.
Entity Framework Core Application Types
Entity Framework (EF) Core supports two application types they are
- Code-Firts
- Database-First
Entity Framework Core mostly goals to the Code-First type and suggest little support for Database-First type due to the visual designer or wizard for Database Model which is not supported for EF Core.
1. Code-First – In the Code-First approach the Entity Framework API builds the database and tabulates the data using the migration based on conventions and configuration offered by the classes of the domain. The Code-First approach will be most helpful in (DDD) Domain Driven Design.
2. Database-First – in the Database-First type the Entity Framework Core API builds the context and domain classes based on the available existing database by using the commands of EF Core. This Database-First has only limited support in EF Core as it will not support Visual Designer or Wizard.
Entity Framework Core Model
The data are accessed using the Model. The Model builds up on the entity classes and the Objects of context which represent the session with the database. The Context objects enable to storing and querying of the data. Entity Framework supports the development approaches model, they are
- From the Existing Database, it generates the Model
- To hand-code the Model to match with the database.
- Migration enables the evolution of the database as Model Changes, if the Model is created make use of EF Migrations to create the database from the Model.
Sample Program
using System.Collections.Generic; using Microsoft.EntityFrameworkCore; namespace SampleModel { public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer( @"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True"); } } public class Blog { public int Blog_Id { get; set; } public string Blog_Url { get; set; } public int Blog_Rating { get; set; } public List<Post> Blog_Posts { get; set; } } public class Post { public int Post_Id { get; set; } public string Post_Title { get; set; } public string Post_Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } } }
Entity Framework Core Database
EF Core is an improved version of ADO.NET which gives developers an automated mechanism for retrieving and storing the data in the database. The Entity Framework EF is used with the .NET Core applications. Entity Framework (EF) Core makes use of the provider model to right to use various databases. EF Core comprises the providers from the NuGet Packages which we have to install.
Let’s see the following table which explains the Database Providers and the NuGet Packages for the Entity Framework Core,
Database Name NuGet Package Name
Conclusion
In this article, we have learned various functionalities and overview,s and features of EF Core which supports DB Providers. It is an Open-Source ORM based Framework for .NET applications and it is an extensible version which is used popularly for the data access technology.
Recommended Articles
This is a guide to Entity Framework Core. Here we discuss the Introduction, overview, features, application types, Entity Framework Core Database with implementation. You may also have a look at the following articles to learn more –