Skip to content

How to create a database in ASP.net core

how to create a database in asp.net core
Become a developer with our complete learning paths
Become a developer with our complete learning paths

In this step-by-step guide, we will walk you through the process of how to create a database in ASP.NET core using Entity Framework (EF) Core, a popular object-relational mapper (ORM) tool.

Creating a database in ASP.NET MVC can seem like a daunting task for beginners, but with the right guidance, it can be a straightforward process.

Let’s get started!

 

You can also watch the video version of this article on YouTube here:

 

 

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:

how to create a database in asp.net core

how to create a database in asp.net core

 

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.

 

how to create a database in asp.net core

how to create a database in asp.net core

 

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

 

 

By the way, did you know that we offer a unique and powerful online course that boosts your C# career? Check it out here!

 

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:

  1. Depending on the initially selected Visual Studio template, chances are that you already have a connection string inside of your appsettings.json file.
  2. 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.
  3. 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.
  4. 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).

 

program.cs

 

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!

 

Conclusion

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.

If you want to skyrocket your C# career, check out our powerful ASP.NET full-stack web development course that also covers test-driven development and C# software architecture.

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Become a senior C# developer and build software like an A-Player
Join the C# Progress Academy and master C# .NET, ASP.NET, Design Patterns, Asynchronous Development, Clean Code, and much more.
Become a senior C# developer and build software like an A-Player
Join the C# Progress Academy and master C# .NET, ASP.NET, Design Patterns, Asynchronous Development, Clean Code, and much more.