Updated April 3, 2023
Introduction to ASP.NET Core Entity Framework
ASP.NET Core Entity Framework is an Open-Sourced framework that depends on the ORM (Object Relational Mapping) Model. Entity Framework is an enhanced version of ADO.NET, which gives developers less work to its an automated system for storing and retrieving the data in the database. The Entity Framework Core is the latest Object-Database Mapper for .NET Framework. Entirely it maintains for Schema Migrations, LINQ Queries, Change Tracking, and any Updates. Entity Framework works with different databases, which includes SQL Database (Azure and on-premises), PostgreSQL, Azure Cosmos DB, SQLite, and MySQL. Entity Framework is proposed to be used with .NET Core Applications.
ASP.NET Core Entity Framework Overviews
- Entity Framework Core is an Open-Source, Extensible, Cross-Platform and Lightweight version of data accessing method. EF allows developers to work with data in the form of domain-specific properties and objects. The Entity Framework Core serves as Object Relational Mapper, which allows the .NET developers to work with DB using .NET objects; it reduces the requirement of lines of code for data accessing. In addition, it supports for several database engines.
- The Entity Framework (EF) Core is the implementation of the Microsoft ORM Framework. The Web Applications are developed using by EF and do not directly work with databases. Instead, those application works with the API of Entity Framework for operations in the related database. EF is an Open-Sourced framework that depends on the ORM (Object Relational Mapping) Model. This framework automatically eliminates the programmer’s work for establishing the connections in a database, retrieving the data from a database, storing the database.
Creating ASP.NET Core Entity Framework
Initially, create the database, and then create a new ASP.NET Core Application in Visual Studio; once selecting the application template, just give the suitable name, location for the project; after giving the appropriate name, click on the ok button as shown below.
Then view project structure and folder creations, the default view of the project looks like as follows.
After that, let’s add three folders called Models, Views, Controllers to support for MVC architecture; for adding the folders, right-click on the project name select Add – New Folder.
After that, set the connection string in ASP.NET Core to set the connection string in the appsettings.json file.
Code:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"MbkDbConstr": "Data Source=(localdb)\\MBK;Database=MbkTest;"
}
}
To create the models, We are going to create the following models.
Let’s see the code for EmployeeViewModel.cs.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace EmployeeList_EF_DbFirst.Models
{
public class EmployeeViewModel
{
[Key]
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public string PhoneNumber { get; set; }
public string Skill { get; set; }
public int YearsExperience { get; set; }
}
}
EmployeeContext.cs:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace EmployeeList_EF_DbFirst.Models
{
public class EmployeeContext : DbContext
{
public EmployeeContext(DbContextOptions<EmployeeContext> options)
: base(options)
{
}
public DbSet<tblSkill> tblSkills { get; set; }
public DbSet<tblEmployee> tblEmployees { get; set; }
}
}
Likewise, code for the remaining models given above table, and add the HomeController class as follows:
Code:
public HomeController(EmployeeContext dbContext)
{
_dbContext = dbContext;
}
public IActionResult Index()
{
var _emplst = _dbContext.tblEmployees.
Join(_dbContext.tblSkills, e => e.SkillID, s => s.SkillID,
(e, s) => new EmployeeViewModel
{ EmployeeID = e.EmployeeID, EmployeeName = e.EmployeeName,
PhoneNumber = e.PhoneNumber, Skill = s.Title,
YearsExperience = e.YearsExperience }).ToList();
IList<EmployeeViewModel> emplst = _emplst;
return View(emplst);
}
Once developing the application completely, execute the project, and the result will be as shown below:
Output:
Set Up ASP.NET Core Entity Framework
When working with the database, we are using the Entity Framework it rewrites the work with the latest .NET Framework.
While working on EF, there will be various things to note they are:
- For the application, we use the SQL Server LocalDB; we can also use the other database; from the remote access database, we can also create a new database on instances.
- The LocalDB is a unique edition of SQL Server for developers.
- Visual Studio 2015 and its Community edition by default install the LocalDB.
- For checking the LocalDB, just go to View – SQL Server Object Explorer option in VS.
Let us now edit the project.json file directly by adding the following two packages:
Setting Up the Entity Framework:
For using the EF initially, we need to install the Entity Framework NuGet Package from NuGet Package Manager or directly through the project.json.
Let’s see the project.json file through by including the following two packages:
The EF Commands package, which helps to perform tasks with EF, includes creating DB schema; those tasks are available at the command-line tool where the logics are present at EF Command Packages.
The EntityFramework.Commands package helps us perform tasks with the Entity Framework, like creating a database schema based on our C# Entity classes, and these tasks are available from a command-line tool where the logic lives inside the EntityFramework. Commands package. For using this command-line tool to add an entry in the command section of project.json as follows.
Look at the following implementation of the project.json file.
Code:
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"EntityFramework.Commands": "7.0.0-rc1-final"
}
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},
"exclude": [
"wwwroot",
"node_modules"
],
"publishExclude": [
"**.user",
"**.vspscc"
]
}
Install Entity Framework
For installing Entity Framework, there will be various options they are as follows:
1. Visual Studio NuGet Package Manager Dialog
- In this just Go to Visual Studio menu choose Project – ManagerNuGet Packages.
- Then click on Browse the Updates Tab.
- To update or install SQL Server provider, choose Microsoft.EntityFrameworkCore.SqlServer package and confirm it.
2. Visual Studio NuGet Package Manager Console
In the menu item, select Tools – NuGet Package Manager – Package Manager Console.
To install the SQL Server Provider execute the following command in the Package Manager Console.
- To Install-Package Microsoft.EntityFrameworkCore.SqlServer.
- To update the provider, make use of Update – Package Command.
- To use the specific version, use the Version Modifier.
Conclusion
In this article, we have seen the ASP.NET Core Entity Framework, which is a lightweight version of Microsoft EF. The Entity Framework Core works with various platforms like Linux, Windows, or iOS.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET Core Entity Framework” was beneficial to you. You can view EDUCBA’s recommended articles for more information.