title: Code first ignore virtual properties
body: <p>I have my db some relations between entities. For example house-rooms:</p>
  
  <pre><code>public class House{
      [Key]
      public int Id{get;set;}
      public string Address{get;set;}
      public virtual IList&lt;Room&gt; Rooms{get;set;}
  }
  
  public class Room{
      [Key]
      public int Id{get;set;}
      public string RoomName{get;set;}
      [ForeignKey("House")]
      public int HouseId{get;set;}
      public virtual House House{get;set;}
  }
  </code></pre>
  
  <p>Ok, now when I query for my room like this:</p>
  
  <pre><code>Room room = ctx.Room.Where(x =&gt; x.Id == myId).FirstOrDefault();
  </code></pre>
  
  <p>If I try to serialize it I have the room, with the house not null, which has inside a list of rooms, with a house, with a list of rooms... and so on.</p>
  
  <p>I want the Room to select only non-virtual properties, so for example the HouseId but not the House itself... how can I achieve this?</p>
  
  <p>EDIT:
  I forgot yo say that I might need to include those nested entities sometimes. For example sometimes I might need to check a property inside the house inside the room. but sometimes not. Is there a way to choose everytimes?</p>