concept one - to - one relationship in category entity framework

This is an excerpt from Manning's book Entity Framework Core in Action.
In EF Core, you can include a class in the application’s DbContext that inherits from another class in the application’s DbContext. For instance, you could’ve defined the
PriceOffer
class as inheriting theBook
class. That would have achieved a similar result to the one-to-one relationship shown previously. EF Core can provide this via the table-per-hierarchy (TPH) configuration, covered in chapter 7.
You want to create a one-to-one relationship without navigational links going both ways (see section 7.6.1).
Figure 7.5 shows the three options for building this sort of one-to-one relationship. The principal entities are at the top of the diagram, and the dependent entities are at the bottom. Note that option 1 has the
Attendee
as the dependent entity, whereas options 2 and 3 have theTicket
at the dependent entity.Figure 7.5 The three ways of defining a one-to-one relationship in a relational database; comments at the bottom indicate EF Core’s handling of each approach. The difference between option 1 and option 2 (and 3) is that the order of the two ends of the one-to-one relationship are swapped, which changes which part can be forced to exist. In option 1, the
Attendee
must have aT
icket
, whereas in options 2 and 3, theT
icket
is optional for theAttendee
.![]()
The
IsRequired
method is most useful in shadow properties because EF Core, by default, makes shadow properties nullable, and theIsRequired
method can change them to non-nullable. Listing 7.9 shows you theAttendee
entity class used previously to show a one-to-one relationship, but showing two other one-to-one relationships that are using shadow properties for their foreign keys.Listing 7.9 The
Attendee
entity class showing all its relationshipspublic class Attendee { public int AttendeeId { get; set; } public string Name { get; set; } public int TicketId { get; set; } #1 public Ticket Ticket { get; set; } #2 public OptionalTrack Optional { get; set; } #3 public RequiredTrack Required { get; set; } #4 } #1 Foreign key for the one-to-one relationship, Ticket #2 One-to-one navigational property that accesses the Ticket entity #3 One-to-one navigational property using a shadow property for the foreign key. By default, the foreign key is nullable, so the relationship is optional. #4 One-to-one navigational property using a shadow property for the foreign key. You use Fluent API commands to say that the foreign key isn’t nullable, so the relationship is required.