2

I have a controller that updates values in a database using Entity Framework. Unfortunately, when I run my application it doesn't seem to work at all. When I put breakpoints in and step through a specific part of the code, it works perfectly.

Here's my controller code:

public ActionResult ManageGame(int id, FormCollection collection, string[] selectedPlayers)
    {
        var gameToUpdate = db.Games
            .Include("Teams")
            .Where(g => g.ID == id)
            .Single();

        if (TryUpdateModel(gameToUpdate, "", null, new string[] { "Players" }))
        {
            try
            {
                List<Player> team1Players = generateRandomTeam();
                List<Player> team2Players = generateRandomTeam(); 

If I put a breakpoint here and step through the rest of the code it's fine, otherwise nothing gets saved.

                foreach (var team in gameToUpdate.Teams)
                {
                    if (!team1added)
                    {
                        team.Players = team1Players;
                        team1added = true;
                    }
                    else
                    {
                        team.Players = team2Players;
                    }
                }

                db.Entry(gameToUpdate).State = EntityState.Modified;
                db.SaveChanges();
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }
        }

        try
        {
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

I have a feeling it's the way I'm assigning the new teams to the existing context, but from all the tutorials I've read, this is the way they do it, at least for string values. Does anybody know why I'm getting this bizarre behavior?

*UPDATE* SOLVED

I solved my problem. My hunch was right, I just needed to add team.Players.Clear() before assigning the new group of players to the existing team.

foreach (var team in gameToUpdate.Teams)
{
    if (!team1added)
    {
        team.Players.Clear()
        team.Players = team1Players;
        team1added = true;
    }
    else
    {
        team.Players.Clear()
        team.Players = team2Players;
    }
}

When I didn't have that, I got a primary key violation exception. Unfortunately I didn't see this exception, because my code was swallowing this as pointed out by DarK. So, after adding the Clear() method everything worked like a charm.

amhed
  • 3,649
  • 2
  • 31
  • 56

1 Answers1

3

Looks like other people have had the same problem like yours. Have alook at these links: C# code only gives expected results on step through?, Code runs correctly only when stepping through it with debugger?

So, if you are instantiating the Random class more than once, you will get some weird results.

EDIT:

From your code, it looks like you're consuming the exception. Can you possibly comment out the try-catch and run it without debugging and see if it throws any exceptions?

Community
  • 1
  • 1
DarK
  • 221
  • 1
  • 3
  • 16
  • I was using two instances of Random and replaced it with one, but I'm still getting the same issue. – user2056006 Feb 17 '13 at 02:17
  • Just once. Then I pass it to other methods with ref Random name. – user2056006 Feb 17 '13 at 02:23
  • Can you please try not using Random and create a dummy object to see if it works without stepping through? This will help you pin-point that there is definitely a problem with the Random object – DarK Feb 17 '13 at 02:26
  • Good idea. Okay, I did that and I still have the same problem. – user2056006 Feb 17 '13 at 02:44
  • From your code, it looks like you're consuming the exception. Can you possibly comment out the try-catch and run it without debugging and see if it throws any exceptions? – DarK Feb 17 '13 at 02:55
  • Your right. It threw a 'Violation of PRIMARY KEY constraint' exception. Thanks for the help so far, now I know what to look for. It's still weird that it works only when debugging. I have a feeling it has something to do with lazy loading. So when I debug, it loads because it has to for me to see the value. I'll have to look into this further. Thanks again! – user2056006 Feb 17 '13 at 03:05