2

I have a custom Board entity with a 1:N relationship to a custom Board Seat entity. I have a post-update plugin on the Board entity that tries to loop through all the Board Seats related to the Board being updated. I've tried both a pre and post image and the relationship is null on both even though the Board has several Board Seats associated with it.

var board = EntityImage.ToEntity<my_boards>();
foreach (var seat in board.board_to_boardseat_relationship)
{
    // Process each seat
}

I'm using strongly typed entities and the type of board.board_to_boardseat_relationship is System.Collections.Generic.IEnumerable<my_boardseat>. It appears that the relationship simply isn't getting populated in either the pre or post image. When I register the image I select all attributes. Any idea how to populate this relationship?

1 Answer 1

2

Mike,

It doesn't get populated because all the attributes are only the attributes of the Entity. You'd have to use some kind of retrieve function to get all the seats associated with this particular board. They are never included in your Post- or Pre-Image (or Target) Below is some sample code:

Entity PostImage = (Entity)m_localcontext.PluginExecutionContext.PostEntityImages["PostImage"]; my_board board = PostImage.ToEntity();

 var seatsList = orgContext.CreateQuery<my_boardseat>().Where(c => c.boardId.Id == board.boardId).ToList();

 if (seatsList.Count > 0)
 {
      foreach (my_boardseat seat in seatsList)
      {
        //Your Code Here
      }
 }
2
  • It can be used for logging. And why no if? is it because the foreach doesnt get executed when the list is empty? Oct 24, 2012 at 9:04
  • 2
    If you want to do something else with the if such as capture some logging than thats fine. But if its just to avoid iterating over an empty collection then theres no need because nothing inside the foreach will be executed. E.g. stackoverflow.com/questions/3914346/…
    – James Wood
    Oct 24, 2012 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.