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”.
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.
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
“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.
Leave a Reply