locked
EF 4 Many-to-Many eager load problem... RRS feed

  • Question

  • Hi,

    I have three tables. Listing, Tag and ListingTag. ListingTag is an association table with only two columns (ListingId, TagId). These two columns are the primary key.

    My edmx shows a many to many relationship between my Listing and Tag entities. It is properly mapped to the ListingTag table.

    I am trying to do an eager load.  My service call is this:

    return this.ObjectContext.Listings.Include("Tags");
    

    My metadata looks like this:

    [Include]
    [Association("ListingTag", "ListingId", "TagId")]
    public EntityCollection<Tag> Tags { get; set; }
    

    The service call returns the Listings but not the Tags. I am using VS 2010 and .NET 4.0.

    Why is my call not returning the Tags?

    Thanks in advance.

    Tuesday, February 15, 2011 11:15 AM

Answers

All replies

  • Hi splitpete,

    Welcome to EF forums.

    Please try in this way:

    =================================

    return this.ObjectContext.Listings.Include("ListinTags.Tag");//the paths are the navigations of entities
    
    =================================
    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.

    Wednesday, February 16, 2011 6:02 AM
  • Thank you for your reply.


    I have tried what you suggested but it throws an error. There is no navigation property called ListingTag (or ListingTags) on the Listing entity. The Listing entity has a Tags navigation property and the Tag entity has a Listings navigation property.

    The relationship between Listings and Tags is the same as the relationship shown in the first diagram on this site:


    http://smehrozalam.wordpress.com/2010/06/29/entity-framework-queries-involving-many-to-many-relationship-tables/

    The cooresponding entity model on that page for students and courses is also the same as mine for Listings and Tags. This site shows how to use linq queries on entities that have been modeled this way but does not show how the data is loaded. I would assume in this example it is being lazy loaded. However, I need to load things up front.

    I have found some sites that suggest that I split the many to many relationship in the edmx into two one-to-many's but I this seems to be a workaround for older versions of entity framework. Also, other sites suggest adding a dummy field to my ListingTag database table in order to force entity framework to create an entity for the relationship. I am hoping I do not have to do either of these.

    Thanks again for your response. Any other ideas?

    Wednesday, February 16, 2011 9:22 AM
  • Hi splitpete,

    Thanks for your feedback.

    I code a a project here: 下载

    I think Include("Tags"); works.

    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.

    Thursday, February 17, 2011 4:02 AM
  • Hi Alan,

    Thank you for your response. I tried your project and it does work. However, the structure of my project is slightly different. I am using RIA Services and Silverlight.

    I used the same tables from your project and created a silverlight application using ria services. When I called the service to return Listings with Tags, I got the correct data on the server side (before it went across the service) and the wrong data after it went across (on the application side)

    I tested this on my original project as well. The data is fine before it goes across the RIA Service. It is not there at all or wrong when it gets to the Silverlight application.

    I would be interested to know if you have the same problem or if this is a known problem.

    I appreciate your help on this issue.

    Any thoughts?

    Thursday, February 17, 2011 11:18 AM
  • Hi splitpete,

    Thanks for your feedback.

    I don't have experience about RIA service, I think it may relates to Serializing object(http://msdn.microsoft.com/en-us/library/06c77f9b-5b2e-4c78-b3e3-8c148ba0ea99).

    By the way, You can repost your question here : http://forums.silverlight.net/forums/53.aspx experts live in.

    Thanks for understanding.

    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.

    • Marked as answer by splitpete Friday, February 18, 2011 10:43 AM
    Friday, February 18, 2011 2:22 AM
  • Hi Alan. I looked through those forums and they seemed to tell me that this is not possible over RIA services. A workaround is necessary.

    The two best solutions I found are:

    Use an intermediate entity

    http://forums.silverlight.net/forums/p/90385/346173.aspx#346173

    A beta from codeplex:

    http://m2m4ria.codeplex.com/

    Thanks again. I appreciate your time and effort.

    • Marked as answer by splitpete Friday, February 18, 2011 10:43 AM
    Friday, February 18, 2011 10:42 AM
  • Hi splitpete,

     I’m glad to hear that you got it working. Thank you for sharing your experience here.

    It will be very beneficial for other community members having the similar questions.

     

    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, February 21, 2011 7:30 AM
  • Hi Alan,

    I came across this thread and have exactly the same issue.  The link to your code project is no longer working and I was wondering if you could re-post it?  Thanks in advance for your help.

    Dan

    Tuesday, November 15, 2011 3:55 PM