Entity Framework – Multiple Properties to same entity

Just a little note to explain how to mark up an EF class that has multiple related properties of the same type. You have to set the ForeignKey attribute to the virtual property you want related. Add-migration will add cascadeDelete which will not work so set this to false.

public virtual
Address HomeAddress { get; set; }
public int HomeAddressId { get; set; }

public virtual Address WorkAddress { get; set; }
public int WorkAddressId { get; set; }

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.