Data Platform Developer Center > Data Platform Development Forums > ADO.NET Entity Framework and LINQ to Entities > Association properties not populated for entities returned by a stored procedure
Ask a questionAsk a question
 

QuestionAssociation properties not populated for entities returned by a stored procedure

  • Saturday, November 07, 2009 10:00 PMPando Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have written a stored procedure that returns data and mapped it to an existing entity type, which happens to have association properties, relating it to other entities. The association properties are populated properly if I query the entity itself (as expected, since the entity corresponds to a real table in the database), but when I query the function that is mapped to my stored procedure, the resulting entity does not have its association properties populated. Am I doing something wrong?

    Thanks,
    Pando

All Replies

  • Monday, November 09, 2009 6:35 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Pando,

     

    Could you explain the “association properties” you mentioned?

    If you mean the navigate properties, that is because the scenario does meet the requirement to get navigate properties.

     

    Only when the parent and children is in the objectcontext, you can get navigate properties.

     

    For example,

    var q= slecect p in context.Parents.Include(“Children”) where …  select p;

     

    In that way, the parent and children are all in the context. Then you can use,

    Foreach(var p in q)

    {

          Foreach(var c in p.Children)

          {

               ………

          }

    }

     

     

     

    Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Monday, November 09, 2009 6:31 PMNoam Ben-Ami - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    In EF4, if you expose any of the tables' foreign keys in your model, and return them through your stored procedure, the EF should fix up the relationships for you using these foreign key values. Of course, this only works for EF4, not EF3.5. In EF3.5, you will need to load the relationship references, e.g. Foo.BarReference.Load().
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Tuesday, November 10, 2009 5:55 PMPando Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The terminology I used in my original post was incorrect. By association properties I meant both the navigation properties and the EntityReference.EntityKey properties. Both are null. I realize that the navigation properties should be null, but I don't understand why the EntityReference.EntityKey properties are also null. I would think that this will make it impossible to load the associated entities (or even get access to their foreign keys).

    By the way, I captured the EF-generated SQL query that calls the stored procedure in SQL Server Profiler, and verified that if I run it manually the foreign key fields are populated properly in the resulting data.

    Thanks,
    Pando