title: EF6 Collection was modified; enumeration operation may not execute
body: <p>I need to add adin users into a table (and child table) where they don't already exist.</p>
  
  <p>I have the following code which throws the error: "Collection was modified; enumeration operation may not execute." on the line "_userPrefsContext.UserRecs.Add(userRec);"</p>
  
  <pre><code>var allUserRecs = _userPrefsContext.UserRecs.ToList();
  
          foreach (string adminUser in adminUsers.Where(x =&gt; x.Length &gt; 0))
          {
              domain = adminUser.Split('\\')[0];
              login = adminUser.Split('\\')[1];
  
              var userRec = new Domain.UserPrefs.BamUser()
              {
                  AdLogonDomain = domain,
                  AdLogonId = login,
                  UserViews = colUserView
              };
  
              if (allUserRecs.Where(x =&gt; x.AdLogonDomain == domain
                                      &amp;&amp; x.AdLogonId == login).Count() == 0)
              {
                  //don't currently have a user rec for this login so create one
                  _userPrefsContext.UserRecs.Add(userRec);
              }
          }
          _userPrefsContext.SaveChanges();
  </code></pre>
  
  <p>I have seen similar posts on SO but in each case they seem to be iterating over the collection into which they are adding. In my case, I'm just trying to iterate over a list of strings.</p>
  
  <p>Where did I go wrong?</p>
  
  <p>Now edited to the following (still the same error):</p>
  
  <pre><code>            List&lt;string&gt; adminUsers = WebConfigurationManager.AppSettings["AdminUsers"].Split(';').ToList();
          adminUsers = adminUsers.Where(x =&gt; x.Length &gt; 0).ToList();
  
          string domain = "", login = "";
          string userViewString = Helpers.ViewNames.UserView.ToString();
  
          int viewId = _userPrefsContext.ViewRecs.Where(x =&gt; x.Name == userViewString).Select(x =&gt; x.BamViewId).Single();
  
          Domain.UserPrefs.UserView userView = new Domain.UserPrefs.UserView()
          {
              BamViewId = viewId
          };
  
          List&lt;Domain.UserPrefs.UserView&gt; colUserView = new List&lt;Domain.UserPrefs.UserView&gt;();
          colUserView.Add(userView);
  
          var allUserRecs = _userPrefsContext.UserRecs.ToList();
          string[] users = new string[adminUsers.Count()];
          int y = 0;
          foreach(string user in adminUsers)
          {
              users[y] = user;
              y++;
          }
  
          int total = adminUsers.Count;
          for (var i = 0; i &lt; total; i++)
          {
  
              domain = users[i].Split('\\')[0];
              login = users[i].Split('\\')[1];
  
              if (allUserRecs.Where(x =&gt; x.AdLogonDomain == domain
                                      &amp;&amp; x.AdLogonId == login).Count() == 0)
              {
                  //don't currently have a user rec for this login so create one
                  _userPrefsContext.UserRecs.Add(new Domain.UserPrefs.BamUser()
                  {
                      AdLogonDomain = domain,
                      AdLogonId = login,
                      UserViews = colUserView
                  });
              }
          }
          _userPrefsContext.SaveChanges();
  </code></pre>