- Lab
-
Libraries: If you want this lab, consider one of these libraries.
Guided: Modeling and Relationships with EF Core 10
In this Code Lab, you'll learn advanced data modeling techniques in EF Core 10. You will start with a basic ASP.NET Core application and enhance its data model by implementing Complex Types, mapping JSON data, and configuring many-to-many relationships. The application uses an in-memory database, so no external database setup is required.
Lab Info
Table of Contents
-
Challenge
Introduction and Setup
Welcome to this Code Lab on Advanced Data Modeling with EF Core 10! In this lab, you'll work with a starter ASP.NET Core application scaffold for a university registrar. Your goal is to enhance the application's data model to handle more complex, real-world data requirements.
The starter project uses an in-memory database, so you can focus entirely on data modeling without needing to set up an external database. You will learn to apply essential techniques like Complex Types, JSON column mapping, and many-to-many relationships. Get started!
info> If you get stuck, you can find solution files for each task in the
solutionfolder of your filetree.This lab experience was developed by the Pluralsight team using Forge, an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us.
-
Challenge
Step 2: Creating the Initial Data Model
The foundation of any data-driven application is its domain model. You'll start by defining the basic entities for your University Registrar API:
StudentandCourse. You'll also register these entities with EF Core'sDbContextso they can be managed and queried. -
Challenge
Step 3: Refactoring to Complex Types
Your initial
Studentmodel has several properties for the address (Street, City, etc.). This is common, but it can be improved. By grouping these properties into a separateAddressclass and marking it as aComplexType, you create a more organized and reusable domain model. EF Core maps the properties of theAddressobject to columns on theStudentstable, avoiding the need for a separateAddressestable. -
Challenge
Step 4: Mapping JSON Data
Some data is semi-structured, like a student's academic history. EF Core lets you map a .NET object to a single JSON column. This keeps your model flexible and still queryable.
Before starting Step 4, complete Steps 2 and 3.
-
Challenge
Step 5: Configuring Many-to-Many Relationships
The relationship between students and courses is a classic many-to-many scenario. A student can enroll in many courses, and a course can have many students. You'll implement this using a join entity called
Enrollment. This approach is powerful because theEnrollmententity can also contain additional information about the relationship, such as theGradea student received in a course. -
Challenge
Step 6: Command-Line Smoke Test
In this step, you'll run a command-line smoke test that starts your API in the background and exercises it with real HTTP requests. This gives you an end-to-end verification that your application runs and routes correctly.
- Open
scripts/api-smoke-test.shand review what it does (start the API, wait for readiness, call key endpoints, then shut the API down). - Run the script:
bash scripts/api-smoke-test.shIf the script fails, it prints the HTTP call that failed and the API output log to help you diagnose the problem. Congratulations on completing the lab! You built an advanced EF Core model using complex types, JSON mapping, and a many-to-many relationship with a join entity—then validated the API end-to-end from the command line with a smoke test.
- Open
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.