Entity Framework Core And Its Data Modelling Approaches

Introduction

 
In this article, I will explain about the term Entity Framework Core. I will demonstrate how to install Entity Framework Core inside ASP.Net Core Application. We will also discuss advantages and disadvantages of various approaches of Entity Framework Core to model the data.
 

Entity Framework Core

 
Entity Framework Core, which is also known as EF Core, is an Object Relational Mapper designed for ADO.Net. It is based on entity classes. Without writing any data access code, entity framework can be used to create and modify data using data specific objects. With the help of Entity Framework core, we can implement a data model and create a database using Entity Framework core data migrations feature.
 
The Entity Framework core Object Relational Mapping techniques together with ASP.Net Core can be used to perform read and write operations on data.
 

Installing Entity Framework Core

 
In order to use Entity Framework core inside ASP.Net Core, we can install a few packages necessary for Entity Framework core to work in an ASP.Net Core project. We can install these packages inside dependencies section of whatever project we want to make.
 
In order to install Entity Framework core inside ASP.Net Core application, we need to follow certain steps as below.
  • Right click on the project name.
  • Select Manage Nuget Packages.
  • We need to ensure that package source drop down list is set to ALL.
  • Then by clicking on the browser tab and searching for required packages starting with Microsoft.EntityFrameworkCore keyword.
 
On clicking the browse tab, we can select and install the following packages as mentioned below.
  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.EntityFrameworkCore.SqlServer
Installing these three packages would also bring some dependencies inside the project, which we need to install in order for Entity Framework Core to work properly.
 
 
 
After installing Entity Framework Core, in order to model the data, we can choose one of the below mentioned approaches which are Model-First, Database-First and Code-First.
 

Model-First

 
With the help of Model-First approach, we can define a model that is an EDMX diagram with the help of design tools. The Entity Framework Core can then use that model or EDMX diagram to auto generate the data model source code files and SQL scripts. So we can say that with Model-First approach, visual diagrams of models are created first and then Entity Framework Core creates and updates SQL scripts and required source code files accordingly.
 
 
 
Advantages 
  • When the data source is quite big, then the visual designing tool can be used to create entity class diagram and database schema.
  • Without any data loss, the model can be modified whenever the database changes.
Disadvantages
  • In case of updates, the loss of data can occur when using diagram driven approach and auto generated SQL scripts.
  • We would not be able to get whatever we wanted because the actual source code will be auto generated by a tool. So we can say that whenever we want model classes according to our needs, this diagram driven approach is not useful.

Database-First

 
With a Database-First approach, we can build a database first in any DBMS engine and then let Entity Framework Core to create and manage all the necessary source code files and model classes along with entity properties.
 
 
Advantages 
  • If we are already having a database created for us then it would save us some time and we do not have create it again. We can work on writing code for the connection of SQL Server with ASP.Net Core and also writing code for other middleware of the pipeline.
  • There will be no data loss because any data model update or any kind of structural change will be performed inside the database.
Disadvantages
Instead of using code driven approach such as migrations features of Entity Framework Core, updating the database manually can be tedious sometimes if the data source is too big.
 

Code-First

 
With a Code-First approach, we can define objects of models and write entity classes that we will be using in our application first and then with the help of code driven approach such as migrations features of Entity Framework Core, we can let Entity Framework Core  generate the database accordingly.
 
 
Advantages
 
There is no need for the diagram driven approach especially for small sized projects as it would save us a lot of time. We just have create model classes and their respective properties using code driven approach and database is created accordingly.
 
Dis-advantages
  • In order to follow the Code-First approach, good knowledge of C# and EF conventions is required.
  • Creating, updating and restoring migrations for handling updates inside the database can be confusing sometimes when maintaining the database.

Summary

 
In this article, I have explained about the term Entity Framework Core. I have demonstrated how to install Entity Framework Core inside ASP.Net Core Application. We have discussed various approaches of Entity Framework Core to model the data. We have also discussed advantages and disadvantages of all the mentioned approaches of Entity Framework Core to model the data.