• About Me
  • Contact Us
  • Privacy Policy
  • Terms and Conditions
  • Advertise / Sponsor

DOTNET DETAIL

Learn Microsoft .NET Technologies

  • Home
  • Angular
    • Angular 5
    • Angular 6
  • Azure
  • ASP.NET Core
  • React
  • Vue
  • Cloud Hosting
  • Software
  • Interview Questions
    • Angular 6
    • ASP.NET Core
    • Microservices Interview Questions
You are here: Home / ASP.NET Core / What is Entity Framework Core in ASP.NET Core 2.0

What is Entity Framework Core in ASP.NET Core 2.0

May 17, 2018 by mebakar1005 Leave a Comment

In this tutorial, we will take a look at Models, Entity Framework, and Migration in ASP.NET Core 2.0 Application. This is the very important tutorial because this ASP.NET Core tutorial will help you to connect your asp net core 2.0 application with database.

Previous Tutorials in the series of ASP.NET Core 2.0

  • Introduction to ASP.NET Core 2.0
  • How to create a new project in ASP.NET Core 2.0 using Visual Studio.
  • Project Folder structure and responsibility of each file in ASP NET Core 2.0 Application.
  • Middleware in ASP.NET Core.
  • Routing in ASP.NET Core.
  • Attribute-based Routing in ASP.NET Core.
  • Tag Helpers in ASP NET Core 2.0
  • Action Results in ASP.NET Core 2.0

What will you learn in this Tutorial?

  • What is a model and How to add in ASP.NET Core Application?
  • What is entity framework in ASP.NET Core?
  • How to add entity framework in ASP NET Core Application?
  • How to add a database to SQL Server using migration?

Let’s move ahead.

What is a model?

MVC is a design pattern to develop a web application. This design pattern separates the application into three parts Model, Views, and Controller. So, we will define the only model in this section according to our heading. Model is basically a business layer or contains a business logic. So, it is a set of classes that describe the data and manipulate the data according to business logic.

How to add a model in ASP NET Core Application?

This is very simple, just follow the below steps.

Step 1: Go to solution explorer => right click on Models folder => choose add => then choose class. Then it will open a new popup window with the name of “Add New Item – StudentRecord”.

READ  How to implement Search Functionality in Angular 6 & ASP.NET Core Application

Step 2: Then select code under the ASP.NET Core from left pane => then choose the class from middle pane => then enter a name like “student.cs” => finally click Add button. It will add a new class into your project under the “Models” folder with the name of “student.cs”.

Step 3: Now its time to add some properties that we need to our project. As you know I am creating a student record application. So, I will use these below properties to maintain student record.

What is Entity Framework Core?

Entity Framework (EF) is a data access technology. It is also known as O/RM (Object Relational Mapper) which enable the .net developer to work with database and work as a middleware between database and data. Entity framework makes it easy to connect with database and reduce the code that the developer needs to write to connect with database. So, the Entity Framework core is the latest version of Entity Framework which is lightweight and cross-platform. It supports many database engines.

How to install Entity Framework Core?

Note: – If you have created the project with ASP NET Core template, then you don’t need to install entity framework because it has already installed it for you. But if you have created the empty project, then follow the below steps. Click here to check how I created the project.

Step 1: go to solution explorer => right click on project name => choose “Manage NuGet Packages…”. It will open a new window. So, enter the “Entity Framework” in the search bar. Then select the entity framework that you want to install and press the install button.

READ  How to create React and Asp.Net Core App using Visual Studio 2017

Step 2: Then it will open a new popup window. Just press Accept button. It will install entity framework in your project.

Add Db Context Class

Now, we will create a new class with the name of ApplicationDbContext class which will implement Db Context so we can setup Database.

Step 1: Go to solution explorer => right click on Models folder => choose add => then choose class. Then it will open a new popup window with the name of “Add New Item – StudentRecord”.

Step 2: Then select code under the ASP.NET Core from left pane => then choose a class from middle pane => then enter a name like “ApplicationDbContext.cs” => finally click Add button. It will add a new class into your project under the “Models” folder with the name of “ApplicationDbContext.cs”.

Step 3: Now replace the below code the ApplicationDbContext class.

Note: – Don’t forget to add references to above the code.

Step 4: Add a Connection string Service in Startup file.

Go to startup.cs file and add the below code into the ConfigureServices() method.

Step 5: Add Connection String

Go to solution explorer => open appsettings.json and then add the below code just above the logging.

“App setting connection string code”

How to add a database in SQL Server using Migration

Let’s see in step by step.

Step 1: Click on “Tools” tab from above menu in visual studio => choose “NuGet Package Manager” => then choose “Package Manager Console”. It will open a new window. Now just right this below command into the package manager console.

  • add-migration AddStudentRecord
READ  Routing in ASP.NET Core 2.0 Application

“AddStudentRecord” is the name of the migration. Then you will see, it will add a new folder with the name of “Migrations” which have two .cs files. And the AddStudentRecord.cs file will create a table in SQL server.

Now, you need to run one more command which is update database. Copy the below command and paste into package manager console and click the enter button.

  • Update-database

So, after some time, you will see the migration has been updated successfully. Now go to SQL server, you will see a database with the name of “StudentRecord” just as below in the screenshot.

Note:- If you found this blog helpful then: Buy me a coffeeBuy me a coffee
Action Results in ASP.NET Core 2.0
Complete CRUD operations in ASP.NET Core 2.0 Application

Related

Filed Under: ASP.NET Core Tagged With: ASP.NET CORE 2.0, ASP.NET Core Models, ASP.NET CORE TUTORIAL, ENTITY FRAMEWORK, MIGRATION

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Buy me a coffeeBuy me a coffee

Best Asp.Net Hosting

Join Us

Join Us

Subscribe to Blog via Email

Enter your email address to subscribe.

One of the best material to learn Angular

Recent Posts

  • Best Asp.Net Hosting Services in 2021
  • SOA vs Microservices Architecture
  • Best Cheap cloud hosting in 2021
  • Top Microservices Interview Questions and Answers
  • Best Cloud Hosting Services in 2021
  • Implement API Versioning in Asp Net Core 5 Web API

Tags

.NET CORE .NET Core 2.0 .NET Core 3.0 .NET Core 3.1 .NET Core 5 2FA ANGULAR ANGULAR 5 ANGULAR 5 TUTORIAL Angular 6 Angular 6 Tutorial Angular 7 Angular 7 Tutorials ANGULAR AND .NET CORE ANGULAR TUTORIAL ASP.NET ASP.NET CORE ASP.NET CORE 2.0 ASP.NET Core 2.1 Asp.Net Core 3.0 Asp.Net Core 3.1 Asp.Net Core 5 ASP.NET Core Authentication ASP.NET Core MVC ASP.NET Core Routing ASP.NET CORE TUTORIAL ASP.NET MVC6 Authentication Authentication and Authorization Azure CRUD Deploy Asp.Net Core App to azure Deployment EF Core ENTITY FRAMEWORK Entity Framework Core Interview Questions JWT Microservices MVC 6 Reactjs Two Factor Authentication User Secrets Visual Studio 2019 WEB API
Copyright © 2021