C# error in Parallel.ForEach
<p>I have a strange problem that I am unable to debug.  Here's a delegate that parses information:</p>

<pre><code>Action&lt;XElement, String&gt; ParseXMLInfo = (s, t) =&gt; {
            using (var Ctx = new Entities ()) {
                var Records = s.Elements ("record");
                Parallel.ForEach (
                    ParallelEnumerable.Range (0, Records.Count ()),
                    u =&gt; {
                        var el = Records.ElementAt (u);
                        try {
                            var NTR = new tbl_UserInfo ();
                            NTR.first_name = el.Element ("first_name").Value;
                            NTR.last_name = el.Element ("last_name").Value;
                            Ctx.AddTotbl_UserInfo (NTR);
                        } catch (Exception excp) {
                            Console.WriteLine (System.DateTime.Now + " " + excp.Message);
                        }
                    }
                );
                Ctx.SaveChanges ();
            }
        };
</code></pre>

<p>The delegate itself is called twice, as follows:</p>

<pre><code>Parallel.Invoke (
            () =&gt; {
                var XMLDoc_MaleInfo = XElement.Load ("MaleNames.xml");
                Console.WriteLine ("Fetching records from MaleNames.xml; starting at " + System.DateTime.Now);
                ParseXMLInfo (XMLDoc_MaleInfo, "male");
            },
            () =&gt; {
                var XMLDoc_FemaleInfo = XElement.Load ("FemaleNames.xml");
                Console.WriteLine ("Fetching records from FemaleNames.xml; starting at " + System.DateTime.Now);
                ParseXMLInfo (XMLDoc_MaleInfo, "female");
            }
        );
</code></pre>

<p>Everything appears to run correctly.  In particular, the Parallel.ForEach portion of the delegate runs without any error.  But then the code breaks at the line Ctx.SaveChanges (), with the message "Object reference not set to an instance of an object."  But when I hover over Ctx (in this broken state), Ctx is not shown to be null.</p>

<p>Can someone please tell me what is going on?  Thanks in advance.</p>
