title: Two to many relationship in ASP.Net MVC EF
body: <p>I'm using EF for the first time and I'm in trouble for setting up a two to many relation between 2 entities.</p>
  
  <p>The 2 entities are 'Customer' and 'Order'.</p>
  
  <p>A customer has many orders.
  An order has two customers : an invoiced customer and a delivered customer.</p>
  
  <p>Here is my models :</p>
  
  <p><strong>Customer</strong></p>
  
  <pre><code>public class Customer
  {
      [Required]
      public int CustomerID { get; set; }
      [Required(ErrorMessage = "Le code client est obligatoire.")]
      public string Code { get; set; }
      [Required(ErrorMessage = "La raison sociale est obligatoire.")]
      [StringLength(35, ErrorMessage = "La raison sociale ne peut excéder 35 caractères.")]
      public string CompanyName { get; set; }
      public bool IsExported { get; set; }
  
      public virtual ICollection&lt;Order&gt; Orders { get; set; }
      public virtual ICollection&lt;Address&gt; Addresses { get; set; }
      public virtual ICollection&lt;Contact&gt; Contacts { get; set; }
  }
  </code></pre>
  
  <p><strong>Order</strong></p>
  
  <pre><code>public class Order
  {
      [Required]
      public  int OrderID { get; set; }
      [Required]
      [ForeignKey("InvoicedCustomer")]
      public int InvoicedCustomerID { get; set; }
      [Required]
      [ForeignKey("DeliveredCustomer")]
      public int DeliveredCustomerID { get; set; }
      [Required]
      public int PaymentTermsID { get; set; }
      [Required]
      [DataType(DataType.Date)]
      [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
      public DateTime OrderDate { get; set; }
      [Required]
      public string OrderRef { get; set; }
      public bool IsExported { get; set; }
  
      public virtual ICollection&lt;OrderLine&gt; OrderLines { get; set; }
      public virtual Customer InvoicedCustomer { get; set; }
      public virtual Customer DeliveredCustomer { get; set; }
      public virtual PaymentTerms PaymentTerms { get; set; }
  }
  </code></pre>
  
  <p>I am able to create an Order and both InvoicedCustomer and DeliveredCustomer are set on the creation.</p>
  
  <p>But I can't access Customer.Orders, I understand that the way I'm doing, EF think that an order belong to only one customer and this customer is not set on order creation.</p>
  
  <p>I don't want to set a many-to-many relationship between those entities because an order has 2 customers only.</p>
  
  <p>I'm looking for a way to access Customer.Orders in customer views, I can't figure how to do.</p>
  
  <p>I've try this :</p>
  
  <p><strong>In CustomerController</strong></p>
  
  <pre><code>var orders = db.Orders.Where(o =&gt; o.InvoicedCustomer == customer
                                            || o.DeliveredCustomer == customer).ToList();
          customer.Orders = orders;
  </code></pre>
  
  <p><strong>In Views.Customer.Details</strong></p>
  
  <pre><code>@foreach (var item in Model.Orders)
  {
      &lt;div class="row"&gt;
          &lt;div class="col-md-3"&gt;
              &lt;dl class="dl-horizontal"&gt;
                  &lt;dt&gt;Référence&lt;/dt&gt;
                  &lt;dd&gt;@Html.DisplayFor(modelItem =&gt; item.OrderRef)&lt;/dd&gt;
                  &lt;dt&gt;Client facturé&lt;/dt&gt;
                  &lt;dd&gt;@Html.DisplayFor(modelItem =&gt; item.InvoicedCustomer.CompanyName)&lt;/dd&gt;
                  &lt;dt&gt;Client livré&lt;/dt&gt;
                  &lt;dd&gt;@Html.DisplayFor(modelItem =&gt; item.DeliveredCustomer.CompanyName)&lt;/dd&gt;
              &lt;/dl&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;hr /&gt;
  }
  </code></pre>
  
  <p>Unfortunetly this is not working.</p>
  
  <p>Any idea to access orders of a customer, assuming that both InvoicedCustomer and DeliveredCustomer must have the order accessible ?</p>
  
  <p>I hope my problem is well detailed.
  Thank you !</p>