title: How to define one-to-one or zero relationship in EntityFramework
body: <p>I have a <strong>Student</strong> entity that has a foreign key <strong>HouseID</strong> which refers to column <strong>GeneralID</strong> in general table. Am i defining it correctly in my model?</p>
  
  <p><strong>Student.cs</strong></p>
  
  <pre><code>public class Student
  {
      public int studentID { get; set; }
      public Nullable&lt;int&gt; houseID { get; set; }
      public virtual House House { get; set; }
    }
  
   public class House
  {
      [Key, ForeignKey("Student"), Column("GeneralID")]
      public int houseID { get; set; }
  
      public virtual Student Student { get; set; }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>
  My OnModelCreating() in GEDBContext class contains</p>
  
  <pre><code> modelBuilder.Entity&lt;Student&gt;()
              .HasOptional&lt;House&gt;(s =&gt; s.House)
              .WithOptionalDependent(h =&gt; h.Student);
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I have changed the Student and House classes as follows:</p>
  
  <pre><code> public class House
  {
      [Key, Column("GeneralID")]
      public int houseID { get; set; }
  
      public virtual ICollection&lt;Student&gt; Students { get; set; }
  }
  
  public class Student
  {
      public int studentID { get; set; }
  
      [ForeignKey("HouseID")]
      public Nullable&lt;int&gt; houseID { get; set; }
      public virtual House House { get; set; }
    }
  </code></pre>