3

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?

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • 1
    Easiest way would propably be to create a DTO-model for serialization. That also hides your DB structure. See this for more info: http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5 – Sami Feb 19 '16 at 12:01
  • Possible duplicate of [Ignoring a class property in Entity Framework 4.1 Code First](http://stackoverflow.com/questions/10385248/ignoring-a-class-property-in-entity-framework-4-1-code-first) – NineBerry Feb 19 '16 at 12:01
  • I suspect that lazy loading is playing tricks on you when serializing, so maybe you should just disable lazyloading using ctx.Configuration.LazyLoadingEnabled = false; and then you will get the result you are after. – jakobandersen Feb 19 '16 at 12:02
  • My problem is that (probably) sometimes I might need those properties, It's better to esclude them and eventually populate them manually or is there a way to decide if including or not them every times? – Pier Giorgio Misley Feb 19 '16 at 12:05
  • @Sami maybe your answer is the solution i'm looking for. If there is a "simpler" way for it like chosing which property include or not every times I will prefer but elseway your will be the solution :) – Pier Giorgio Misley Feb 19 '16 at 12:08
  • @Pier: look at the answer I linked to above. The second part of the answer dealing with "Fluent API" is what you are looking for. – NineBerry Feb 19 '16 at 12:09
  • @NineBerry Doesn't that just ignore the DB mapping for a property? It's still there when serialized..? – Sami Feb 19 '16 at 12:12
  • You can disable the caching of the model, so that the event OnModelCreating is called for each single instance, so that you can decide for each single instance whether you want to map a certain property or not – NineBerry Feb 19 '16 at 12:14
  • @NineBerry can I ask you a little sample on how to istantiate every time only the properties you want? – Pier Giorgio Misley Feb 19 '16 at 13:03
  • See https://msdn.microsoft.com/de-de/library/system.data.entity.dbcontext.onmodelcreating%28v=vs.113%29.aspx for reference – NineBerry Feb 19 '16 at 13:05

0 Answers0