EF6 Collection was modified; enumeration operation may not execute

entity-framework entity-framework-6

Question

I need to add adin users into a table (and child table) where they don't already exist.

I have the following code which throws the error: "Collection was modified; enumeration operation may not execute." on the line "_userPrefsContext.UserRecs.Add(userRec);"

var allUserRecs = _userPrefsContext.UserRecs.ToList();

        foreach (string adminUser in adminUsers.Where(x => x.Length > 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 => x.AdLogonDomain == domain
                                    && x.AdLogonId == login).Count() == 0)
            {
                //don't currently have a user rec for this login so create one
                _userPrefsContext.UserRecs.Add(userRec);
            }
        }
        _userPrefsContext.SaveChanges();

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.

Where did I go wrong?

Now edited to the following (still the same error):

            List<string> adminUsers = WebConfigurationManager.AppSettings["AdminUsers"].Split(';').ToList();
        adminUsers = adminUsers.Where(x => x.Length > 0).ToList();

        string domain = "", login = "";
        string userViewString = Helpers.ViewNames.UserView.ToString();

        int viewId = _userPrefsContext.ViewRecs.Where(x => x.Name == userViewString).Select(x => x.BamViewId).Single();

        Domain.UserPrefs.UserView userView = new Domain.UserPrefs.UserView()
        {
            BamViewId = viewId
        };

        List<Domain.UserPrefs.UserView> colUserView = new List<Domain.UserPrefs.UserView>();
        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 < total; i++)
        {

            domain = users[i].Split('\\')[0];
            login = users[i].Split('\\')[1];

            if (allUserRecs.Where(x => x.AdLogonDomain == domain
                                    && 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();
1
1
5/29/2017 11:45:29 AM

Accepted Answer

UserViews is defined as a domain entity as follows:

public virtual ICollection<UserView> UserViews { get; set; }

To make this work I changed from

var userRec = new Domain.UserPrefs.BamUser()
            {
                AdLogonDomain = domain,
                AdLogonId = login,
                UserViews = colUserView
            };

tovar userRec = new Domain.UserPrefs.BamUser() { AdLogonDomain = domain, AdLogonId = login, UserViews = colUserView.ToList() };

To be honest, I don't know why the .ToList() is needed - if anyone does then I think a explanation may be helpful to many!

2
5/29/2017 1:20:28 PM

Popular Answer

Just for suggestion its better your are using any replace of count. any working much faster than count. use this code

 if (!allUserRecs.any(x => x.AdLogonDomain == domain                                   && x.AdLogonId == login))
            {
                //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
                });
            }

Replace if

if (allUserRecs.Where(x => x.AdLogonDomain == domain
                                    && 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
                });
            }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow