title: How to create two way navigation property in entity framework?
body: <p>There is a <code>Cargo</code> class/table which has identity <code>CargoID</code> 
  There is a <code>ContainerIn</code> class/table which containes  <code>CargoID</code> </p>
  
  <p>Every Cargo could have 1 or 0 corresponding container entries. 
  I am trying to create navigation properties such that. 
  <code>Cargo.ContainerIn</code>--->should give me associated <code>ContainerIn</code> entry
  <code>ContainerIn.Cargo</code>--->should give me associated <code>Cargo</code> entry</p>
  
  <p><strong>Cargo Class:</strong></p>
  
  <pre><code>public class Cargo
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int CargoID { get; set; }//SerialNo
      [Required]
      public DateTime DateOfPassage { get; set; }
      public string CompanyUserName { get; set; }
      public virtual ContainerIn ContainerIn { get; set; }
  }
  </code></pre>
  
  <p><strong>ContainerIn Subclass:</strong></p>
  
  <pre><code>public class ContainerIn 
  {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ContainerInID { get; set; }
    public int CargoID { get; set; }
    public virtual Cargo Cargo { get; set; }
    public int LoadStatus { get; set; }
  }
  </code></pre>
  
  <p>I have also tried adding <code>public int ContainerInID { get; set; } in</code>Cargo` class.
  I am still getting :</p>
  
  <pre><code>`Unable to determine the principal end of an association between the types 'PisMark3.Models.Cargo.ContainerIn' and 
  'PisMark3.Models.Cargo.Cargo'. 
  The principal end of this association must be explicitly configured
   using either the relationship fluent API or data annotations.`
  </code></pre>
  
  <p><strong>EDIT:</strong>
  I have added <code>OnModelCreating</code> in  <code>ApplicationDbContext</code> class. </p>
  
  <pre><code> public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;PisMark3.Models.Cargo.Cargo&gt;()
                          .HasOptional(s =&gt; s.ContainerIn)
                          .WithRequired(ad =&gt; ad.Cargo);
          }
          public ApplicationDbContext()
              : base("DefaultConnection", throwIfV1Schema: false)
          {
              //  Database.SetInitializer&lt;ApplicationDbContext&gt;(new DropCreateDatabaseIfModelChanges&lt;ApplicationDbContext&gt;());
          }
      .... 
  </code></pre>
  
  <p>Now I am getting:
  <a href="https://i.stack.imgur.com/axzMr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/axzMr.png" alt="enter image description here"></a></p>