Adding More Properties to our Course Model
In this lesson, we're going to add more properties to our course model
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:07] So far in our course model we have used properties that are going to be displayed on the home page. Just the essential ones.
[00:08 - 00:15] But that's not enough. Our course model needs to grow so that we can show everything about a course in its description page.
[00:16 - 00:30] Let's go to our course entity. And first thing I would like to create is a base entity which will carry only the ID since IDs are something common between all the models.
[00:31 - 00:43] All the models will then derive from the base entity. Let's create a new class inside the entity project and let's call it base entity.
[00:44 - 00:52] Press enter. On the namespace it gives us a dot but we want the namespace to be entity and now the error is gone.
[00:53 - 01:08] Our base entity just needs to have the ID property. So we can go to the course property and cut it from here and we can paste it to our base entity.
[01:09 - 01:19] And let's import quid from system. Now this model should derive from the base entity.
[01:20 - 01:33] Now whenever you are creating an entity and you want the ID to be of type quid you can derive it from the base entity. Now let's discuss what else our course model should have.
[01:34 - 01:40] First thing I can think of is the subtitle. And the subtitle will be of type string.
[01:41 - 01:52] So string here and subtitle. We also need description that will be of type string as well.
[01:53 - 02:01] The number of students enrolled in the course which will have a type int. So let's keep it int and let's call it students.
[02:02 - 02:08] We need the language of the course. So that will be string and language.
[02:09 - 02:21] Also the level of the course if it's for beginners, intermediate or advanced students. So type string again and level.
[02:22 - 02:40] We need requirements of the course which will be more than one most of the times. What we can do is drop and make an i collection, i collection of requirements.
[02:41 - 02:49] Now let's create the new class using quick fix. That's in both system.collection first.
[02:50 - 02:59] And this i collection will be of type requirement. Let's create this one.
[03:00 - 03:05] Generate class requirement in new file. Okay, let me correct spelling first.
[03:06 - 03:15] It should be requirement. Let's use the quick fix and we need to generate it in a new file.
[03:16 - 03:30] Okay, now this will have an ID which will be of type int and name which will be of type string. Let's call it name.
[03:31 - 03:43] Let's create a copy of type date. Let's import it.
[03:44 - 03:55] And course property of type course. This is to create the relationship.
[03:56 - 04:08] We saw an error because we did not import system.collections.generic. Now the same way we also need learnings of the course which will again be of type i collection.
[04:09 - 04:16] But this time it will be of type learning. So let's change the name to learning.
[04:17 - 04:26] And we'll have to create a class again in a new file. So generate type learning in a class learning in a new file.
[04:27 - 04:36] We need the same property as requirement. So let's copy it and we'll check here.
[04:37 - 04:42] And the errors are gone. Now we need two more tables, requirements and learnings.
[04:43 - 04:52] Let's go back to the store context.cs file and copy this twice. Because we need two more.
[04:53 - 05:02] pv sets one is for requirements. So let's call it requirement. And this one will be learning.
[05:03 - 05:08] So let's write learning here. Since we are using the code first approach.
[05:09 - 05:13] The joining of the table will take place automatically. Going back to the course entity.
[05:14 - 05:21] We also need category of the course. The category property will be special because we want to later on filter the courses based on the category.
[05:22 - 05:28] Since we are using the code first approach. We just need to create a new category class and map them together.
[05:29 - 05:35] Let me explain. A course can have one category but one category will have lots of courses in it .
[05:36 - 05:46] For example, if I want to filter out courses in the music category. We just need to go to our category table and find the courses associated with music.
[05:47 - 05:54] Let's create the relationship now. First of all, we need to add a category ID.
[05:55 - 06:02] Which will be of type int. And also we need to add property.
[06:03 - 06:14] Which will be called category and it will be of type category as well. This is required to create the one to many relationships.
[06:15 - 06:21] We see an error. So let's create new category using QuickPix.
[06:22 - 06:28] In a new file. Inside the class, I need an ID of type int.
[06:29 - 06:35] So int and a TV ID. You can use the type GED as well.
[06:36 - 06:42] But I have created a seed file with ID as numbers. Such as 1, 2, 3 and so on.
[06:43 - 06:48] For making it easier to understand. That's why I'm using integer here rather than using the GED.
[06:49 - 06:53] I also need name of the category. Which will be of type string.
[06:54 - 07:05] So type is string and it's called name. Since one category can have multiple courses associated with it.
[07:06 - 07:15] I need to make an i-collection of course. So this will be i-collection of course.
[07:16 - 07:26] And I will name it courses. We need to import i-collection using system.collections.generic.
[07:27 - 07:32] And that's simply it. In the code first approach, this is what we have to do.
[07:33 - 07:45] And of course, migrate it to make the changes appear in our migrations. Before making the migration, let's check our course entity once again and see if we need something else.
[07:46 - 07:54] I think we can add last updated property. Whenever we make any changes to the course, we will update the last updated property as well.
[07:55 - 08:09] So this will be of type date time. And the name will be last updated.
[08:10 - 08:24] We can add the initial value by calling it date time.now. This date time.now will take the current time at the time of creation.
[08:25 - 08:28] Let's make the migration now. Open the terminal.
[08:29 - 08:37] First of all, make sure that you are inside the base directory and not inside the API project. Now, first of all, we need to drop the database.
[08:38 - 08:44] And for that, we need to type .net if. Database.drop.
[08:45 - 09:07] And our starter project is API and the name of this project is going to be infrastructure. Confirm it by typing Y.
[09:08 - 09:15] Now we can remove the previous migration using .net if. Migrations.Remove.
[09:16 - 09:36] Again, the starter project is API and the name of the project where our database is located is the infrastructure project. Finally, let's add the new migration by typing .net if.
[09:37 - 09:41] Migrations.Add. And after that, we need to name the migration.
[09:42 - 09:54] Let's call it course properties. And again, the starter project and infrastructure project.
[09:55 - 10:06] We see the new files being created. Now, let's go through these migration files.
[10:07 - 10:17] We have category stable on top with columns ID and name. After that, we have the courses table with all the properties that we have mentioned.
[10:18 - 10:37] If we move down a bit, we can see a foreign key associated with the category stable. We have two more tables, Learning Stable and the Requirements table with ID and name and the course ID with foreign keys on the courses table.
[10:38 - 10:44] So, everything is exactly as we wanted. But I want our migration to change something.
[10:45 - 10:52] For example, I don't want my strings to be nullable. Let's see how to change the configuration of the migrations in the next lesson.