Introduction to Object-Relational Mapping

Currently our app only saves data while it is running. Once you end the session and close the program, all of the data you provided disappears. We can make that data persistent by connecting your application to a database. This will retain the data even if the application is not running, and allow you to access it when you open a new session.

Let’s connect our MVC application to a relational database and add persistent data storage to our apps. To do so, we need to use object-relational mapping.

Object-Relational Mapping or ORM is a technique for converting data between C# objects and relational databases. ORM converts data between two incompatible type systems (C# and MySQL), such that each model class becomes a table in our database and each instance a row of the table.

To make ORM work in our C# applications, we need an object-relational mapper to convert between C# and MySQL. When we create and configure a new model class to be stored in a database, a mapper creates a MySQL query to make the corresponding table.

Example

Let’s think about this in CodingEvents.

In CodingEvents, we have the Events class that contains the following properties:

  • Id
  • Name
  • Description
  • ContactEmail

Now we want to store this information in a MySQL database. We can use ORM so that the database of our application has a table to contain all objects instantiated from the Events class.

The table called Events has four columns:

  • integer Id
  • varchar Name
  • varchar Description
  • varchar ContactEmail.

Now, let’s instantiate a C# Events object:

1
2

public Events codeWithPride = new Events("Code With Pride", "Creating community for LGBTQIA+ technologists and allies", "[email protected]")

Recall, the Id is set by the constructor at this time. We will learn more about this soon.

Having set up our application, we can add info about the Code with Pride event to our Events table. While we will write the code to add Code With Pride’s info to our database in C#, the frameworks and APIs that make ORM happen will run the following MySQL query for us:

1
2
INSERT INTO Events (Name, Description, ContactEmail)
VALUES ("Code With Pride",Creating community for LGBTQIA+ technologists and allies", "info@launchcode.org");

Now Code with Pride and all the information we provided is stored in our MySQL database in the Events table!

ORM in ASP.NET

One of the most widely used object-relational mappers available for C# and ASP.NET Core is Entity Framework Core. This framework makes use of data layers. When we learned about models , we learned that data layers add abstraction between models and the data we want to store. With Entity FrameworkCore, data layers take the form of classes that extend DbContext.

Before we add persistence to our data, it’s important to remember that: Models are NOT persistent data stores, and relational databases do NOT shape the C# objects we will be using. We want to make sure that the two remain separate.

Note

We’ll often shorten Entity Framework Core to EF. The “Core” in the name indicates that we’re talking about the version of EF that is compatible with ASP.NET Core.

Adding ORM to CodingEvents

To work with EF Core, we need to add it to the project.

Currently, Visual Studio 2022 (Windows and Mac) is typically installed with EF 6.0.X.

Verify EF Core Tools are Present

If you downloaded Visual Studio 2022 or later, let’s verify EF Core’s presence and version.

You can test that it has been installed in Visual Studio by running the following in your terminal.

  1. cd your way down into the project folders. Verify your location by running the ls command. You should see all the folders within your project.

    student-computer:CodingEvents student$ ls
    CodingEvents.csproj               ViewModels
    Controllers                       Views
    Data                              appsettings.Development.json
    Models                            appsettings.json
    Program.cs                        bin
    Properties                        obj
    Startup.cs                        wwwroot
    
  2. When you are this level run the following command:

    dotnet ef
    

    You should see the followng output:

       student-computer:CodingEventsDemo student$ dotnet ef
    
                _/\__
          ---==/    \\
    ___  ___   |.    \|\
    | __|| __|  |  )   \\\
    | _| | _|   \_/ |  //|\\
    |___||_|       /   \\\/\\
    
    Entity Framework Core .NET Command-line Tools 6.0.X
    
    //code continues ...
    
Note

We recommend using or installing EF Core version 6.0.11 or higher.

Troubleshooting EF Core Tools

If you are not able to see the Entity Framework Core logo, then try the following steps to troubleshoot the issue.

  1. Open a terminal window using your terminal app outside of Visual Studio.

  2. Open a terminal and run:

    dotnet tool install -g dotnet-ef
    

    This command installs a set of command-line tools for working with EF globally, which means it will be available for any ASP.NET project we use in the future. We will use the tools provided by this package to update our database schema after adding or changing model classes.

  3. Once you have taken these steps, you are ready to set up the appropriate models and controllers for the application. We’ll do that in the next section.

  4. To test that this install worked, run dotnet ef. The output should be a message displaying basic EF tool commands and options

    Note

    Note for Mac users only

    For these tools to be accessible from the command line, they must be within your user path. We create or update your bash profile. Your bash profile is a text file that you can add any paths needed. You may add to this as you continue on your programming journey.

    1. Open your ~/.bash_profile with this command:

      open ~/.bash_profile
      

      The ~ symbol is shorthand for your home directory, which is the directory you are in when you open a new terminal window.

    2. Add the following line to the very bottom of your profile:

      export PATH="$PATH:$HOME/.dotnet/tools/"
      
    3. Save and close the file. Then close your terminal window and open a new one, so that the changes can take effect.

    4. To test that this install worked, run dotnet ef. The output should be a message displaying basic EF tool commands and options.

Once you have taken these steps, you are ready to set up the appropriate models and controllers for the application. We’ll do that in the next section.

Check Your Understanding

Question

True/False: An ORM converts data between C# objects and relational databases.