I have my db some relations between entities. For example house-rooms:
public class House{
[Key]
public int Id{get;set;}
public string Address{get;set;}
public virtual IList<Room> 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;}
}
Ok, now when I query for my room like this:
Room room = ctx.Room.Where(x => x.Id == myId).FirstOrDefault();
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.
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?
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?