Creating a Database

In this lesson, we'll creating our database based on the migration

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:09] In the last lesson, we created our first migration using the code first approach. .NET EF tool helped us to create the migration.

    [00:10 - 00:22] Let's see what we can do with the database now. Let's clear the terminal and type .NET EF database -h and press enter.

    [00:23 - 00:35] As you can see, we have an option to update and to drop, which makes it very clear which one we want to use right now. The update command will create the database upon the migration we created in the last lesson.

    [00:36 - 00:45] It is built upon the latest migration and make the changes accordingly to the database. This is our first time and we don't want to look for the changes.

    [00:46 - 00:51] It will create the database from scratch. Well, this is one way of creating it and it certainly works.

    [00:52 - 01:01] Try it if you want to, but we want our database to be filled with some data when it first starts. So what we want is to check if there is any database.

    [01:02 - 01:11] If there is not, we want to create a database and fill it with some initial data. To do this, we'll have to go to a class which is invoked first when the server starts.

    [01:12 - 01:20] And it's a main method which is inside a program.cs class. To set it up, we need to make some changes to our main method.

    [01:21 - 01:33] First of all, we'll have to put this method inside a variable. Let's name it host and remove run for now because we want to set up the migration before we finally run this method.

    [01:34 - 01:39] So our host variable is storing this command. Let's create another variable and call it scope.

    [01:40 - 01:54] This variable will contain host.services.create scope. We see an error which means we have to run the quick fix and call dependency injection to this class.

    [01:55 - 02:01] What we want is to dispose this variable after we have finished using it. We don't want it to be hanging here forever.

    [02:02 - 02:12] So we will use the using keyword before this variable. And what we'll do is it will dispose this variable after the method is correctly executed.

    [02:13 - 02:24] It is one of those things we want to dispose of after we are done using them. So basically it will create a scope for services we are going to use in this method and dispose it after it's done.

    [02:25 - 02:38] Below that we are going to create a variable called services and it will store scope.service provider. Now, we're going to call our database service and there might be a chance of failure.

    [02:39 - 02:58] To handle the errors, let's create a try and catch block. At the try block, let's create a variable called context and it will store our database service.

    [02:59 - 03:15] So it will be services.get required service and it will be of type store context class that we created. Now, let's use the quick fix using command and dot on Mac and control and dot on Windows depending on your OS.

    [03:16 - 03:24] Now, let's select infrastructure and the error is gone. If you remember, we have added the database service in the startup class as well.

    [03:25 - 03:48] In this case, we just want to store that service in the context variable so that we can use the service when the server starts to make sure we are using the latest migration to create a database. Our context gives us a method to do that which is context dot database dot migrate.

    [03:49 - 03:55] Now let's resolve this error using the quick fix. Let's call Microsoft dot entity framework go.

    [03:56 - 04:06] Now it's resolved. If we hover over it, it says it applies any pending migrations for the context to the database.

    [04:07 - 04:16] We'll create the database if it does not already exist and this is perfect for our use case. We want if there are any pending migrations, it should apply that while starting up.

    [04:17 - 04:29] And if there are no databases, it will create it. Now if something bad happens and it fails, for example, if there is no migration or there is some connection problem, we need to handle that.

    [04:30 - 04:37] We have another service called iLogger which simply logs the problem to our console. So let's use it.

    [04:38 - 04:52] Our logger is equal to services dot get required service and the service is i Logger. It takes a type which in this case is our program class.

    [04:53 - 05:03] Then we'll write logger dot log error. First argument will be the exception and second will be the message.

    [05:04 - 05:12] Let's type something wrong happened during migration. This finishes our try catch block.

    [05:13 - 05:17] Do you think it's going to work? I don't think so because we haven't called the run method yet.

    [05:18 - 05:24] So let's do it below the try catch block. Let's write host dot run.

    [05:25 - 05:32] Now it's ready for the action. Let's go back to the terminal and clear everything.

    [05:33 - 05:48] Let's go inside our API project and type dotnet watch run. Let's check out the messages we see if migration history which means our code has worked.

    [05:49 - 05:55] Below that we can see that all our course properties are listed here. I'm so excited to see what it has created.

    [05:56 - 06:09] You see that let's open our command palette once again and type open database and click this option. Now you will see this option only if you have installed the SQLite extension.

    [06:10 - 06:20] If you have not please pause the video and go ahead install the extension and come back. If we click on the option you see our database learnify dot DB is showing.

    [06:21 - 06:35] So let's click on that. In the explorer window there is a SQLite explorer open that and inside learnify dot DB we have our courses table and underscore EF migration history table as well.

    [06:36 - 06:39] It keeps a track on our migrations. Let's open that.

    [06:40 - 06:43] It shows us the migration ID. We currently have just one migration.

    [06:44 - 06:59] When we do more migrations they will be added to this table and we can switch back to previous migrations as well. If we open our courses table we are going to see nothing because we haven't created any data for our courses table.

    [07:00 - 07:05] I know what to do next. In the next lesson let's create some data and seed it to our database.