Defining relationships

Now that we have built our main entity skeleton we need to create some relationships between them. We want to be able to do stuff like retrieving an Item, browsing to their related Comments, then getting the ApplicationUser for each one of them, and also the other way around. We can easily do that by implementing a set of related entity properties: EF will load them on demand using its default lazy-load retrieval feature.

The first thing we'll do is to add a new region to our Item class, containing two new properties:

#region Related Properties /// <summary> /// Current Item's Author: this property will be loaded on first use using EF's Lazy-Loading feature. /// </summary> [ForeignKey("UserId")] public virtual ApplicationUser ...

Get ASP.NET Core: Cloud-ready, Enterprise Web Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.