locked
EF JOIN over WCF RRS feed

  • Question

  • To the best of my reccolection, as I experienced first hand, "lazy loading" and "eager loading" can't be used when trying to serialize data back across the wire with WCF.

    I had to execute two AJAX calls to get data from two separate, but related tables. I then had to do a LINQ-TO-OBJECT query from the parent table to the child table to get what I needed.

    I then had to manually create a separate POCO class (to combine data from both) to show in a grid.

    All of the above was done with EF4.

    Can you use lazy or eager loading with WCF and EF5 when making DB calls or is EF5 capable of creating POCO classes with related tables when doing a JOIN operation?

    I know if WCF wasn't in the mix, I'd be able to use eager or lazy loading and even a JOIN.

    Maybe it can be different using the new WEB API?

    Is anything different about this scenario with EF5?


    Bill Yeager

    Wednesday, August 29, 2012 8:26 PM

Answers

  • Actually you would need to loop through the children and set their State property, then call save changes on the context at the end.

    It would go something like this:

    1. Set modified/unchanged state of parent
    2. Loop through children setting modified state
    3. call SaveChanges on context.

    When calling SaveChanges EF will not do anything unless it is tracking entities that are in a state that requires changing the database. So when getting your entities back you need to create a new context, tell EF about the entities so that it tracks them, and set an appropriate state. You can tell EF to track an entity and set its state at the same time by setting context.Entry(entity).State.

    Information about it is here: http://msdn.microsoft.com/en-us/data/jj592676


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by Bill_Yeager Thursday, August 30, 2012 6:20 PM
    Thursday, August 30, 2012 3:53 PM

All replies

  • To the best of my reccolection, as I experienced first hand, "lazy loading" and "eager loading" can't be used when trying to serialize data back across the wire with WCF.

    I had to execute two AJAX calls to get data from two separate, but related tables. I then had to do a LINQ-TO-OBJECT query from the parent table to the child table to get what I needed.

    I then had to manually create a separate POCO class (to combine data from both) to show in a grid.

    All of the above was done with EF4.

    Can you use lazy or eager loading with WCF and EF5 when making DB calls or is EF5 capable of creating POCO classes with related tables when doing a JOIN operation?

    I know if WCF wasn't in the mix, I'd be able to use eager or lazy loading and even a JOIN.

    Maybe it can be different using the new WEB API?

    Is anything different about this scenario with EF5?


    Bill Yeager

    • Merged by Allen_MSDN Thursday, August 30, 2012 5:27 AM same question
    Wednesday, August 29, 2012 8:27 PM
  • Hi Bill,

    If you are using EF 5 with POCOs then you should be able to use eager loading and return them to your WCF clients without any trouble.

    What is likely to be a problem is working out what changes have been made and getting them to your database when you send objects back.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Wednesday, August 29, 2012 10:08 PM
  • Glenn, since I'm able to use eager loading to get my data to the client, before I send back my entity, I would just update the parent/child records in the entity on the client (which will have a parent/child relationship in the POCO class, via ICollection, when EF5 creates it from the DB). Once I send back my entity, all I should have to do is to update the parent record (do a save), then loop through the child records for the updates saving each child record).


    Bill Yeager

    Thursday, August 30, 2012 3:44 PM
  • Actually you would need to loop through the children and set their State property, then call save changes on the context at the end.

    It would go something like this:

    1. Set modified/unchanged state of parent
    2. Loop through children setting modified state
    3. call SaveChanges on context.

    When calling SaveChanges EF will not do anything unless it is tracking entities that are in a state that requires changing the database. So when getting your entities back you need to create a new context, tell EF about the entities so that it tracks them, and set an appropriate state. You can tell EF to track an entity and set its state at the same time by setting context.Entry(entity).State.

    Information about it is here: http://msdn.microsoft.com/en-us/data/jj592676


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by Bill_Yeager Thursday, August 30, 2012 6:20 PM
    Thursday, August 30, 2012 3:53 PM
  • That's more or less what I was thinking of...

    However, you said it in excellent detail. That was a great link to that I bookmarked.

    Thanks!


    Bill Yeager

    Thursday, August 30, 2012 6:20 PM