Entity Framework Code First: How can I create a One-to-Many AND a One-to-One relationship between two tables?

前端 未结 2 1899
北恋
北恋 2020-11-30 07:06

Here is my Model:

public class Customer
{
    public int ID { get; set; }

    public int MailingAddressID { get; set; }
    public virtual Address MailingAd         


        
2条回答
  •  青春惊慌失措
    2020-11-30 07:30

    I know you're trying to figure out the Entity Framework way of doing this, but if I were designing this I would recommend not even wiring up MailingAddress to the database. Just make it a calculated property like this:

    public MailingAddress {
       get {
          return Addresses.Where(a => a.IsPrimaryMailing).FirstOrDefault();
       }
    }
    

提交回复
热议问题