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; }
[ForeignKey("HomeAddress")]
public int HomeAddressId { get; set; }

public virtual Address WorkAddress { get; set; }
[ForeignKey("WorkAddress")]
public int WorkAddressId { get; set; }

Leave a Reply

Your email address will not be published. Required fields are marked *