concept one - to - one relationship in category entity framework

appears as: one-to-one relationship, one-to-one relationships, one-to-one relationship
Entity Framework Core in Action

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 the Book 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 the Ticket 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 a Ticket, whereas in options 2 and 3, the Ticket is optional for the Attendee.

    c07_05.png

    The IsRequired method is most useful in shadow properties because EF Core, by default, makes shadow properties nullable, and the IsRequired method can change them to non-nullable. Listing 7.9 shows you the Attendee 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 relationships
    public 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.
    sitemap

    Unable to load book!

    The book could not be loaded.

    (try again in a couple of minutes)

    manning.com homepage
    test yourself with a liveTest