locked
in a query with many to many relationships are inclueded a lot of objects RRS feed

  • Question

  • I have a many to many relation between two tables in my database, so in my edmx model I have only two tables, no the intermediate table. This tables are Movies and Genres.

    When I search for some movies, I get also the related entities, so with eager loading I populate the collection Genres of my entities Movies. Is what I want.

    The problem is that when in the result has more than one movie, the collection Genres of the entities Movies are populated, but the collection Movies of the entities Genres are populated too. So when I send to the repository one movie to be updated, if I attach the the entity to the context, I attach this movie and all the movies that have the same genres that the movie that I want to update. This is because in the genres of the entities are included the movies that are of the same genres than the movie that I want to update.

    So if in my first search I get a lot of movies, when I want to update one movie I send a lot of entities that I don't need to seed, so it is a lot of traffic in the network.

    is there any way when I search for results the collection of movies in the genres entities are not populated? Because I only populate the related entities of my main entity, movies, not all the collections of all the secondary entities.

    Thanks.

    Tuesday, June 18, 2013 5:40 PM

Answers

  • Hi ComptonAlvaro,

    EF should be able to detect which entities are modified when DbContext.Configuration.AutoDetectChangesEnabled property is true. Only the added, deleted, and modified entities will be updated to database.

    If you want to explicitly prevent some entities from updating to database, you can also try to DbEntityEntry.State property to Unchanged.

    dbContext.Entry(customer).State = EntityState.Unchanged;
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Chester Hong Tuesday, July 2, 2013 5:51 AM
    Thursday, June 20, 2013 6:26 AM