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.
public static async Task SeedAsync()
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.
public static async Task SeedAsync(StoreContext context, ILogger logger)
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
:
[
{
"Rating": 4.7,
"Instructor": "Chirag Kalra",
"Title": "Fullstack React with Typescript: Become a frontend Master",
"Price": 29.99,
"Image": "https://learnify-assets.s3.amazonaws.com/Images/react-typescript.png"
},
{
"Rating": 4.5,
"Instructor": "Chirag Kalra",
"Title": "Fullstack Angular with a real world Application",
"Image": "https://learnify-assets.s3.amazonaws.com/Images/fullstack-angular.png",
"Price": 29.99
},
{
"Rating": 4.4,
"Instructor": "Chirag Kalra",
"Title": "Backend with Node JS: Write your own server",
"Image": "https://learnify-assets.s3.amazonaws.com/Images/backend-with-node.png",
"Price": 16.99
},
{
"Rating": 4.6,
"Instructor": "Chirag Kalra",
"Title": "Vue JS for beginners: Start your Web dev journey",
"Image": "https://learnify-assets.s3.amazonaws.com/Images/vue-for-beginners.png",
"Price": 11.99
},
{
"Rating": 4.9,
"Instructor": "Chirag Kalra",
"Title": "Docker and Kubernetes: Become a container expert",
"Image": "https://learnify-assets.s3.amazonaws.com/Images/docker-kubernetes.png",
"Price": 29.99
},
{
"Rating": 4.5,
"Instructor": "Chirag Kalra",
"Title": "Fullstack Python with Django: Become a Python expert",
"Image": "https://learnify-assets.s3.amazonaws.com/Images/python-django.png",
"Price": 34.99
},
{
"Rating": 4.5,
"Instructor": "Chirag Kalra",
"Title": "Introduction to Java servlets",
"Image": "https://learnify-assets.s3.amazonaws.com/Images/java-servlets.png",
"Price": 19.99
},
{
"Rating": 4.9,
"Instructor": "Chirag Kalra",
"Title": "Data Structures and Algorithms: Crack SDE Interviews",
"Image": "https://learnify-assets.s3.amazonaws.com/Images/ds-algo.png",
"Price": 29.99
},
{
"Rating": 4.8,
"Instructor": "Chirag Kalra",
"Title": "Become an AWS certified Developer",
"Image": "https://learnify-assets.s3.amazonaws.com/Images/aws-developer.png",
"Price": 24.99
}
]
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...