This video is available to students only

Adding More Properties to our Course Model

In this lesson, we're going to add more properties to our course model

Adding Properties to Course Entity#

So far in our Course model, we have used the properties that are going be displayed on the homepage; just the essential ones though, but is this enough? Nah. Our Course model needs to grow so that we can show everything about a course in its description page. Let's go to our course model and first thing I would like to do is create a base entity which will carry only the id since ids are something common between all the models. All the models will then derive from the BaseEntity.

Let's create a new class inside the Entity project and call it BaseEntity. It just needs to have the Id property, so we can copy the property from Course Model and paste it here. We then need to import System using quick fix. Now all the models we make must derive from the BaseEntity class.


Let's discuss what else our Course model should have. First thing I can think of is the subtitle of type string. We would also need the description again of type string. The number of Students enrolled in the course will have a type int; let's call it Students. We need language of the Course which will be of type string. Level of the course; if its for beginners, intermediate or advanced students, would be of type string again. We need requirements of the course, which will be more than one most of the times. We can make it an ICollection of type Requirement and let's call it Requirements. Let's create a new class, Requirement, and there we need an id, name, CourseId of type Guid and course property of type Course to create the relationship. The same way, we need learnings of the course. Let's copy it and change the name to learnings. Create a new class, Learning, with exact same properties. Now we need two more tables, Requirements and Learnings. Let's go back to the StoreContext.cs file and copy this twice and replace Course with Category and Requirement. Tables' name will be plural of the Entity so it's Requirements and Learnings. Since we're using code first approach, the joining of the table will take place automatically. You will see that Requirements and Learnings table will have CourseId property inside. More on that when we make the migration.




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