Seeding Data in our Database

In this lesson, we'll add initial data to our database

Seeding data in the database#

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 seed data in Entity Framework Core. Let's go back to our Infrastructure project and create a new file. Since this file will help us in seeding the data, let's call it StoreContextSeed.cs. Inside this class, we will create a static method. A static method can be used without creating a new instance of the class. Now let's create the static method, public static async Task and let's call it SeedAsync. Since in this method, we're talking to our database to check if it's empty, we need to make it asynchronous and that's why we have used the async keyword and the task. A task is an object that represents some work that should be done. The task can tell you if the work is completed and if the operation returns a result, the task gives you a result. In this case, we are not returning any response, that's why we have mentioned only Task. Suppose we were expecting a string to be returned, it would have been Task of type string. For all the asynchronous methods we create, we will use the Task object.

Now let's add the parameters. In this method, we need our Store context, so let's write it down. Let's give it a name, context. We also need ILogger to log the information, so let's give it a name, logger. We would see plenty of errors. You know what to do here; let's go to each of them and use quick fix. Let's go to Task, and import from Microsoft.Threading.Tasks and now let's go to ILogger and import it from Microsoft.Extensions.Logging.

We see a little warning because we haven't used anything asynchronous so far; don't worry about it just yet. To handle the errors, let's create a try catch block. First of all, we need to check if there is any data in our database. To do that, we'll use an if statement which checks if our database is empty, so if context.Courses.Any(), we'll use a bang operator which checks the opposite of it. This statement is checking if the database is empty; we want our method to read data from the file and add it to the database. You must have downloaded the Course Assets folder; now open the folder and copy the courses.json file. Inside the Infrastructure project, create a new folder and name it SeedData. Paste the courses.json file inside that folder. If we 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.

SeedData/courses.json:

Let's store this file's data inside a variable and let's name it courseData. Now, if you want to read text from a file, you need to write File.ReadAllText method and inside the method, mention the path. Since this method is going to run from the Program.cs file which is inside the API file, we need to go out from the API directory inside the Infrastructure project, then go to the SeedData folder and finally the courses.json file. We see an error because we haven't imported Syste.IO which provides File method. Let's use the quick fix to import it.

 

Loading...

Start a new discussion. All notification go to the author.

Loading...