Hello
I'm trying to delete all "Usergroup"s that belongs to one "User" and then add new "Usergroup"s.
public void SaveUserUsergroups(int userID, int[] UsergroupID)
{
User uo = _entities.Users.Where(x => x.UserID == userID).First();
uo.Usergroups.Load();
foreach(Usergroup ug in uo.Usergroups)
{
uo.Usergroups.Remove(ug);
}
int _currentUsergroupID;
for (int i = 0; i < UsergroupID.Count(); i++)
{
_currentUsergroupID = UsergroupID[i];
uo.Usergroups.Add(_entities.Usergroups.Where(ug => ug.UsergroupID == _currentUsergroupID).First());
}
_entities.SaveChanges();
}
It throws an exception if there are more than 1 usergroup here:
foreach(Usergroup ug in uo.Usergroups)
{
uo.Usergroups.Remove(ug);
}
How should I fix that?
/M