This step-by-step tutorial will show you how to construct a database in ASP.NET core using the well-known object-relational mapper (ORM) tool Entity Framework (EF) Core.
For newcomers, building a database in ASP.NET MVC can seem like a difficult operation, but with the right direction, it can be simple.
Let’s get going!
Step 1: Create a new ASP.NET MVC project
The first step in creating a database in ASP.NET MVC is to create a new project. In Visual Studio, select “File” > “New” > “Project.”
Under the “Web” tab, select “ASP.NET Web Application” and give it a name. In the next window, select the “MVC” template and click “Create.”
For this guide we use Visual Studio 2022:
Step 2: Install the EF Core package
Entity Framework Core is a separate package that needs to be installed in your project. To do this, right-click on your project in the Solution Explorer and select “Manage NuGet Packages.” In the search bar, select “Browse” type “Microsoft.EntityFrameworkCore” and select the latest version. Click “Install” to add it to your project.
Step 3: Create a model class
A model class is a representation of the data that will be stored in the database. In this step, we will create a model class for a simple “Student” entity. In the “Models” folder of your project (if you have none, create it now), add a new class called “Student.cs.” In the class, add properties such as “Id,” “Name,” and “EnrollmentDate.”
Step 4: Create a context class
The context class is responsible for connecting to the database and managing the entities. To create a context class, add a new class called “SchoolContext.cs” in the “Models” folder.
In the class, inherit from the “DbContext” class and add a “DbSet” property for each entity in the database. In our example, we will add a “DbSet” for the “Student” entity.
Note: In order to successfully setup the database provider later, we need to create the constructor and pass the options object to the base constructor
Step 5: Getting your connection string
For this example, we will use this database connection string:
If you want to connect to your own database, you need to grab your own specific connection string which could look slightly different.
How to find your connection string?
There are a few ways to find your database connection string, depending on how your database is set up and where it is hosted. Some common ways to find your connection string include:
- Depending on the initially selected Visual Studio template, chances are that you already have a connection string inside of your appsettings.json file.
- Look in the database management software: If you are using a local or on-premises database, you can also look for the connection string in the database management software you are using. For example, if you are using SQL Server, you can open the SQL Server Management Studio and look for the connection string in the “Connect to Server” window.
- Check with your hosting provider: If you are hosting your database on a cloud platform such as Azure, Amazon Web Services (AWS), or Google Cloud Platform (GCP), you can check your hosting provider’s documentation or dashboard to find the connection string. Each provider has its own way to manage the connection strings, and usually, you can find them in the databases or connection sections of the providers’ web portals.
- Check your application’s documentation: If you are using a third-party application or software that connects to a database, the connection string may be included in the application’s documentation.
Note: some of the cloud providers have the option of using service endpoints instead of connection strings, in this case, you should check for the endpoint of the service and use it to connect.
Step 6: Configuring a database provider
In our example, we use a SqlServer database. The next step is to set up a database provider in our Program.cs file. Open the NuGet package management window again and install the “Microsoft.EntityFrameworkCore.SqlServer” package (or a similar package if you use a different database provider).
Step 7: Use migrations to create the database
In order to create migrations (database changes) and our database itself we need to install another package. Open the NuGet package management window again and install the “Microsoft.EntityFrameworkCore.Tools” package.
Migrations are a way to keep track of changes to the database schema. We now want to create a new migration that contains all the changes (adding a student entity).
In the Package Manager Console, type “Add-Migration InitialCreate” and press enter. This will create a new folder called “Migrations” in your project with a file called “InitialCreate.cs.”
Step 8: Update the database
The final step is to update the database using the “Update-Database” command. In the Package Manager Console, type “Update-Database” and press enter. This will create the database and the necessary tables (students table).
Note: This will create a new database in the provided server if you have no database set up
The more you know: Migrations are a way of keeping track of your database changes but migrations themself do NOT change the database when created. If you want to apply the database changes, you have to run the “Update-Database” command, which will then execute the actual migrations.
Congratulations you have now created and connected a database in ASP.net core from scratch!
Creating a database in ASP.NET MVC using EF Core may seem like a lot of steps, but once you understand the process, it can be done quickly and easily. With this guide, you should now be able to create a database for your ASP.NET MVC project and start storing data.