1

I am using EntityFramework 5.0 and I have Resharper installed.

I have the following code:

using (var context = DataObjectFactory.CreateCardholdersContext())
{
    var cardholdersWithCards = from cardholders in context.CardholderEntities
                                from cards in context.CardholderCardEntities
                                select new
                                {
                                    Cardholders = cardholders,
                                    Cards = cards
                                };

}

In the 2nd line of my code, I have an underline under context, with a warning access to disposed closure.

What does this mean and how should I change my code?

Null Reference
  • 11,260
  • 40
  • 107
  • 184
  • look [here](http://stackoverflow.com/questions/17620430/access-to-disposed-closure-in-c) or [here](http://stackoverflow.com/questions/19248282/what-does-access-to-disposed-closure-mean-here) or [here](http://stackoverflow.com/questions/14218907/does-this-resharper-fix-for-disposed-closure-warning-make-any-sense) or [here](http://stackoverflow.com/questions/21159776/access-to-disposed-closure-mark-methods-as-safe) or [here](http://stackoverflow.com/questions/13713754/is-linqifying-my-code-worth-accessing-a-foreach-variable-in-a-closure). Did you **really** check for duplicates before posting? – default Jul 18 '14 at 08:37
  • @Default Yes, I googled "Entity framework access to disposed closure" – Null Reference Jul 18 '14 at 08:38
  • There's a ton of duplicates to this.. Skip the Entity framework in your search, since that is not related https://www.google.se/search?q=access+to+disposed+closure. Even if SO loves helping people, you should also learn to help yourself by doing a proper search for the topic before asking. Otherwise you are just adding noise. – Patrick Jul 18 '14 at 08:54
  • Agreed. Should've widened my search scope – Null Reference Jul 18 '14 at 09:04

1 Answers1

1

It is saying the context could have been disposed of before this code gets executed resulting in an error.

You need to post more code to know why.

Beakie
  • 1,948
  • 3
  • 20
  • 46
  • 1
    Try adding a ToList() to the end of the query. This will ensure the data is fully enumerated BEFORE the context is disposed. – Beakie Jul 18 '14 at 08:38
  • 1
    The point is, your code is OK, it's just resharper being overly cautious. This should fix it. – Beakie Jul 18 '14 at 08:40
  • Can you post CreateCardholdersContext()? – Beakie Jul 18 '14 at 08:45
  • @Beakie it's irrelevant, it's the variable in `using` that is causing it, and the fact that the `from` statement could be enumerated later, i.e. **after** the `using` (when `context` is already disposed). It's explained in the multiple questions I linked to in the comments to the question. It is like you said, Re# is simply being cautious. – default Jul 18 '14 at 08:47