17.4. Exercises: OMG the ORM!

For the exercises, we are going to continue building on our CodingEvents application. The exercise instructions assume that your code resembles the persistent-controller branch. Create a new branch in your own CodingEvents repo to get started on the exercises.

Note

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.

17.4.1. The EventCategory Class

First, create a new class called EventCategory in the Models directory.

EventCategory needs to have the following:

  1. An Id property of type int.
  2. A Name property of type string.
  3. A no arg constructor.
  4. A constructor that sets the value of Name.

EventCategory represents data that will be stored in our database.

Check your solutions

17.4.2. 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.

Check your solutions

17.4.3. 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!

Tip

Where is my table!?

If you are not able to see your new EventCategory table try the following:

  1. Drop tables
    1. Start by dropping all of your tables in MySql Workbench
    2. Add a new migration in your terminal
    3. Update your database in the terminal
    4. 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.

  1. Drop schema
    1. Drop the schema in MySql Workbench. Don’t worry, we’ll get it back.
    2. Recreate 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 appsettings.json file as well as either you Program.cs or Startup.cs files.
    3. Delete your old migrations from your C# project.
    4. Add a new migration in your terminal
    5. Update your database in the terminal
    6. Refresh your schema in MySql Workbench

You should see your new table in MySql Workbench now.

17.4.4. 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.

17.4.4.1. Index()

Index() needs to do the following:

  1. Responds to GET requests at EventCategory/Index and returns a view called Index.cshtml.
  2. Pass the DbContext category values as a list into the view template as a model.

Check your solutions

17.4.5. 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:

  1. Use the list passed in from the action method in the controller as a model to populate the view.
  2. An h1 with an appropriate heading for the page.
  3. A table that will display all of the category names of the event categories stored in our database.

Check your solutions

17.4.6. 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 week.