Include with necessary fields only


  • Hi, I'm using EF5, Database First and DBContext (generated using T4).

    I have 2 huge tables linked by a foreign key, I need almost all the fields from the main table but the second one is not important in the class I'm working on, so I would like to select only the important fields (id, name and lastUpdate).

    I need to update the lastUpdate field, but I don't want to select all the fields on the second table just to update it, it has almost 200 fields. I've tried to use MyLinqQueryForTheMainTable.Include("SecondTable") and then Load that query, but that will create a select with all the fields from both table.

    Using TpT I could achieve it, but I also need a POCO with all the fields so I can use it on another class and if I map the same field two times I'll get and edmx error. I can't use lazy loading, the connection is closed and can't be opened again after the "query time", our application need to limit the access to the database to specific times and opening and closing connections all the time is not an option.

    Thank you all in advance.

    Thursday, April 12, 2012 12:59 PM


  • Hi Adauto,

    I'm not very sure about your ways, TPT is build on One To One relationship.  Based on your requirement, to tell your truth, there is not a good way to handle this scenario, my suggestion is dividing the one query into two parts.

    By the way, in EF4.1 code first there is a way to filte related entities: .Query() 

    Have a nice day.

    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 16, 2012 8:38 AM