Seeding Data in our Database
In this lesson, we'll add initial data to our database
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo 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.
Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

[00:00 - 00:15] Now that we have our database set up, it's ready to accept data. We could have created the data using the queries or using the API request, but I wanted to show you how you see data in entity framework core.
[00:16 - 00:27] Let's go back to our infrastructure project and create a new file. Since this file will help us searing the data, let's call it store context seed .cs.
[00:28 - 00:34] Inside this class, we will create a static method. A static method can be used without creating a new instance of the class.
[00:35 - 00:52] Now let's create the static method, a blick, static, async, task, and let's call it seed async. Since in this method, we are talking to our database to check if it's empty, we need to make it asynchronous.
[00:53 - 01:02] And that's why we have used the async keyword and the task keyword. A task is an object that represents some work that should be done.
[01:03 - 01:12] The task can tell you if the work is completed and if the operation returns a result, the task gives you the result. In this case, we are not returning any response.
[01:13 - 01:21] That's why we have mentioned only task. Suppose if we were expecting a string to be returned, it would have been task of type string.
[01:22 - 01:28] All the asynchronous methods we create, we will use the task object. Now let's add the parameters.
[01:29 - 01:33] In this method, we need a store context. So let's write it down.
[01:34 - 01:39] Let's give it a name context. We also need iLogger to log the information.
[01:40 - 01:43] Let's give it a name logger. We see plenty of errors here.
[01:44 - 01:48] You know what to do. Let's go to each of them and use quick fix.
[01:49 - 01:54] Let's go to task. Let's import it from Microsoft dot threading dot tasks.
[01:55 - 02:06] Now let's go to iLogger and import it using Microsoft dot extensions dot logging. We see a little warning because we haven't used anything asynchronous so far.
[02:07 - 02:13] Don't worry about it just yet. Now to handle the errors, let's create a try catch block.
[02:14 - 02:23] First of all, we need to check if there is any data in our database. To do so, we'll use an if statement with checks if our database is empty.
[02:24 - 02:37] So if context dot courses dot any, we'll use a bang operator with checks the opposite of it. So this statement is checking if the database is empty.
[02:38 - 02:45] We want a method to read data from the file and add it to the database. So the statement is checking if the database is empty.
[02:46 - 02:58] We want a method to read data from the file and add it to the database. You must have downloaded the course assets folder, open the folder and copy the courses dot JSON file.
[02:59 - 03:10] Now inside the infrastructure project, create a new folder and name it seed data. Inside that folder paste the courses dot JSON file.
[03:11 - 03:24] If you look at the file, we have some course data which includes rating, instructor, title, price and image. These are the properties we defined inside our course model.
[03:25 - 03:41] Let's store this file inside a variable and let's name it course data. Now if you want to read text from a file, you need to write file dot read all text method and inside the method mentioned the path.
[03:42 - 04:05] Since this method is going to run from program dot CS file, which is inside the API, we need to go out from the API directory and inside the infrastructure project, then the seed data folder and finally the courses dot JSON file. We see an error because we haven't imported system dot IO, which provides file method.
[04:06 - 04:15] So let's use the quick fix and import it. Then what we want to do is to serialize what's inside the courses dot JSON file into our course object.
[04:16 - 04:42] What we can do now is to create a new variable courses and equals to JSON serializer, which we can import from system dot text dot JSON. Once the error is gone, we can say dot de serialize, then we have to mention the type of model, which is the list of course model, then inside it the course data, which has all the JSON data.
[04:43 - 04:56] Let's import list from system dot collections dot generic and course from the entity project. Once it's de serialized, we can store this into our database using the store context.
[04:57 - 05:09] Let's create a for each loop. Let's write var item in courses, which stands for an individual course object.
[05:10 - 05:27] Then we will write context dot courses dot add and then specify the item inside the method. Right now our context has tracked everything and the data is stored in memory, but it's still not added to the database.
[05:28 - 05:39] For that, let's go outside the loop and mention await context dot save changes async. This is where all the data will be stored in the database.
[05:40 - 05:41] That's it. This is what we'll write in the try block.
[05:42 - 05:52] Now let's write our catch block. Catch exception and let's call it X.
[05:53 - 06:04] Let's import exception from system. Inside the catch block, let's use the logger dot log error method, which will display the X dot message.
[06:05 - 06:07] That's it. We are done with this file for now.
[06:08 - 06:26] Let's go back to our program dot CS file and inform it to see the data when the application starts below the context dot database dot migrate method. Let's use await store context seed dot seed async.
[06:27 - 06:39] Let's also pass the context and logger, which is also available in this method. We see an error because we are using await keyword, but our main method does not use the async keyword.
[06:40 - 06:52] Let's add async and replace void with task. Since we are using async keyword now, let's change migrate to migrate async and add await in front of it.
[06:53 - 07:01] We can also change host dot run to host dot run async. Let's add await keyword here as well.
[07:02 - 07:10] It still shows an error because variable logger is in the catch scope. Let's place it above the try and catch block and we are done.
[07:11 - 07:29] Like I mentioned earlier, this is a static method and that's why we don't have to create a new instance to call this method. Now all we need to do is stop the application and restart it to see the course data inside our database.
[07:30 - 07:47] In the command palette type open database inside the SQLite explorer, let's open it inside the course table, right click on it and click on show table. And yes, we finally can see the data inside our table.
[07:48 - 07:52] Perfect. We can see all the properties we seeded inside the database.
[07:53 - 07:58] And in the next lesson, let's get this data using the API. I will see you in the next lesson.