Seeding New Data
In this lesson, we're going to seed data for Category, Learnings and Requirements table
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:16] We have made some new migrations in the previous lectures and now we want to update the database and see some new data in our table. But before that we need to add the category DB set here as well which we forgot to do in the last lecture.
[00:17 - 00:34] Let's open the store context.cs file and let's copy one of it and paste it. Now replace course with category and of course we need to change the spelling of categories.
[00:35 - 00:50] And that's it. Make sure you are inside the root project and again we'll have to delete the previous migration because the category stable was not there.
[00:51 - 01:18] So we will type .NET/EF/migrations/remove, start a project as API and the project was in infrastructure. Now we have to create a new migration which will include the category stable.
[01:19 - 01:48] Let's create a migrations add let's call it category table and start a project and the infrastructure project. Let's open and see if it has created the category stable and see we have category stable here as well.
[01:49 - 02:13] Now what we need to do is update it. So what we will do is .NET/EF/database/update/api/in-perstructure.
[02:14 - 02:38] Now I would like you to copy the JSON files from the transcript below including courses.json file because this time I have added id property to courses.json file. If I don't provide the id our database will create one which will be unique every time and we will have to make changes to our seeding data because the tables now have a relationship with each other.
[02:39 - 02:53] You will find four JSON files. So let's copy the file and copy the file and copy the file and copy the file and copy the file and copy the file.
[02:54 - 03:07] First let's do it for courses.json file remove it and paste the new data. We have the id here the language students level category id everything we mentioned in our table.
[03:08 - 03:18] Also we will have to do it for other files as well. Let's do it for categories.json file.
[03:19 - 03:34] Copy the data inside cdata folder. Let's create another file and let's call it categories.json and just paste it here.
[03:35 - 03:54] Let's do the same for learnings.json file. learnings.json this has course id as well and finally for requirements.json file.
[03:55 - 04:08] Structure for both the tables are same that's why the data type and the properties are same there. requirements.json and paste it here.
[04:09 - 04:19] Now that we have the data ready let's go to the store context seed file which is here. Let me close everything else.
[04:20 - 04:40] Inside store context seed file we just have to implement the data seeding as we did for the courses table. But order is really important here because if you look at the courses.json file we have the category id property which should be present in the category table at the time of table creation.
[04:41 - 04:57] So, so what we need to do is copy this part and paste it above this. We need to replace courses by categories.
[04:58 - 05:38] So because the name of our table is categories here and let's change course data to category data and here it's not courses.json but categories.json. And here it is categories and here as well we want category data and this is going to be list of type category.
[05:39 - 06:12] Now let's change this to categories as well and here it will be categories and yeah everything else looks good. Category data seeding from the categories.json file here as well list of category and we have added the category data for item in categories and we are adding those items inside the category stable.
[06:13 - 06:19] Perfect. Let's do the same thing now for the learning stable and the requirements table.
[06:20 - 06:34] This should be below the courses table because they have course id property which should be available at the time of table creation. Let's copy it and paste it.
[06:35 - 07:30] Next to it for the learning stable first replace courses with learnings and here as well let's call it learning data here we can call it learnings cdata slash the file name is learning stotchison and here it is called learning data and yeah here it's learnings and we need to add it inside the learning stable. Now let's copy it one more time finally for the requirements table.
[07:31 - 08:07] Now courses will be called requirements, course data will be called requirement data the file's name is requirements.json. It's teams courses to requirements and it should be list of requirement and this is not cost data but the requirement data.
[08:08 - 08:25] It's in finally HP requirements and here we want to add it to the requirements table. So we will mention requirements and all the errors are gone.
[08:26 - 08:45] Everything else stays the same we are just reading the data from the file and then deserializing it as per the entity and finally adding it to the database after making it go through the file each loop. Now what we need to do is go to our API project.
[08:46 - 09:02] Let me clear this. Let me go to the API project and run .NET watch run.
[09:03 - 09:21] It's working properly now we can open the database we can open the palette using command shift np on Mac or control shift np on windows and then click on open database. As we can see we have our learnify.dd let's open it and we can see our category stable.
[09:22 - 09:31] We have all these categories here let's open the courses table. We have everything as we mentioned.
[09:32 - 09:42] Now we have learning stable. Yes and finally the requirements table.
[09:43 - 09:46] Everything is working as per the plan. Amazing.
[09:47 - 09:53] Now what we need to do is return the data with our API request. So let's configure that in the next lesson.