Table of Contents

IT:AD:EF/CodeFirst:HowTo:Define/Models/Relationships/Examples/1-0..1

Summary

An invoice could be for the guy at the counter, or could be for a purchase that is to be shipped out.

Therefore the invoice's Shipping Address (of which there is only 1) is optional:

invoiceAddressuserOptional Shipping Addr11Optional Home Addr11

Process

Process

   //////1-0..1:
        modelBuilder.Entity<Invoice>()
            //The property is called Shipping Address, but is backed 
            //by an Address entity
            .HasRequired(i => i.ShippingAddress)
            .WithMany()
            //As the address is probably a reference to the 
            //User's 'home' address, we turn off cascade delete.
            .WillCascadeOnDelete(false); 
            //As the Address table is shared between different
            //entities, we can't put on it a UserFK or InvoiceFK
            //or means to navigate back to the owner table.
            //so avoid .WithRequired()