Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have two entities in 1:n relationship: Link, Category. At the first I get all categories and Links and put them to list. Next I fill the links of every category manually but category.Links.Add(link) connect to db and get the link again and it cause double data in the result. I know this action is because of lazy loading. the lazy loading is true and I do not want to disable it. How can I fill links of every category manually without connecting to db again? Please do not offer Eager loading or disabeling lazy load.

        var categories = categoryRepository.GetCategories().ToList();
        var allLinks = linkRepository.GetLinks().ToList();

        foreach (var category in categories)
        {
            var links = allLinks.Where(l => l.CategoryID == category.CategoryID);

            foreach (var link in links)
            {
                category.Links.Add(link);
            }
        }
share|improve this question
    
Can you explain why do you want this and why do you want this while using lazy loading in the same time? –  Ladislav Mrnka Apr 30 '11 at 13:03
    
Its only a sample. I use this solution for better performance. I do not want to do this while using lazy loading in the same time. I want to use lazy loading in other queries. –  Ghooti Farangi Apr 30 '11 at 17:45

1 Answer 1

up vote 1 down vote accepted

Even you mentioned that you don't want to turn off the lazy loading I insists you to do that because lazy loading will be triggered always when you access the navigation property first time. The only way to avoid this is:

  • Turning lazy loading off. The disadvantage is that you must turn lazy loading off for whole processing of categories. Because once you turn it on it will immediately reload Links during next access to the property. The reason is that EntityCollection used for handling Links has property IsLoaded which is false (and cannot be modified except by calling Load).
  • Removing virtual keyword from Links collection. This will disallow wrapping only Category by dynamic proxy and because of that it will not allow lazy loading. It will be even more interesting because the only needed code in your loading should be first two lines - links will be populated to categories automatically during second query execution. The problem here is that removing virtual keyword from auto generated entities is hard (it must be hard coded to T4 template) or you must use own entity.
  • Somehow cheating EF by replacing EntityCollection in Links property with another collection. This is not easy because you can't just assign new collection to Links - dynamic proxy will throw InvalidOperationException. You must create second partial part of your entity and add some code which will modify directly private field holding the collection (bypassing the property). Even such approach can have some bad consequences. Default code uses some fixups etc.

Turning lazy loading off will not break your functionality - actually EF is doing this quite often internally. You just need to do:

context.ContextOptions.LazyLoadingEnabled = false;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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