Exercises: OMG the ORM!
Exercises
For the exercises, we are going to continue building on our CodingEvents application. The exercise instructions assume that your code resembles the orm1-with-db branch. Create a new branch in your own CodingEvents repo to get started on the exercises.
You will be making one model class and adding to DbContext. If you are not sure what these classes should look like, refer back to the section on data stores and DbContext.
The EventCategory Class
First, create a new class called EventCategory in the Models directory.
EventCategory needs to have the following:
- An
Idproperty of typeint. - A
Nameproperty of typestring. - A no arg constructor.
- A constructor that sets the value of
Name.
EventCategory represents data that will be stored in our database.
Adding to DbContext
Once you have created EventCategory, you need to add to EventDbContext to set up a table in the database for storing categories.
Adding a New Table to the Database
With the two steps completed above, you now need to add a new migration and update your database. You should see a new table in your database!
Where is my table? f you are not able to see your new EventCategory table try the following:
- Drop tables
- Start by dropping all of your tables in MySql Workbench
- Add a new migration in your terminal
- Update your database in the terminal
- Refresh your schema in MySql Workbench
If you still can’t see your table, then you will need to drop your schema and delete old migrations.
Drop schema
- Drop the schema in MySql Workbench. Don’t worry, we’ll get it back.
- ecreate your schema in MySql Workbench like you did earlier. You can use the same names and passwords if you like. If you change any of the names and passwords, make sure you update your MySql connections that you made in the
Program.cs. - Delete your old migrations from your C# project.
- Add a new migration in your terminal
- Update your database in the terminal
- Refresh your schema in MySql Workbench
You should see your new table in MySql Workbench now.
The EventCategoryController Class
Create EventCategoryController in the Controllers directory. To get our action methods working, we also need a variable of type EventDbContext.
For now, we are just going to set up our Index() action method so that it displays all of the categories in our table.
Index Action Method
Index() needs to do the following:
- Responds to GET requests at
EventCategory/Indexand returns a view calledIndex.cshtml. - Pass the
DbContextcategory values as a list into the view template as a model.
Adding a View
Create an EventCategory directory inside of our Views directory. Add a new view called Index.cshtml.
Index.cshtml needs to have the following:
- Use the list passed in from the action method in the controller as a model to populate the view.
- An
h1with an appropriate heading for the page. - A table that will display all of the category names of the event categories stored in our database.
Test Your Application
If you navigate to the /eventcategory route, you will see an empty table on the page. That is what you should see! We haven’t added the ability to add a new category to our table yet. We will do so in the studio this chapter.