views:

48

answers:

2

I've just started foraying into EF, and I'm trying to get my head around it (Generally easy enough). However for some reason it's not actually saving the results, though it appears to be maybe cacheing the values for a time, and then rolling back to the previous values?

using (IAPlayerEntities IAPE = new IAPlayerEntities()) {
                ObjectSet<Player> Players = IAPE.Players;
                if (Players.Count() > 0) {
                    Player currentPlayer = new Player();
                    bool LoggedIn = false;
                    Players.DefaultIfEmpty(null);
                    while (!LoggedIn) {
                        Console.WriteLine("Please enter your Username:");
                        string username = Console.ReadLine();
                        Console.WriteLine("Please enter your Password:");
                        string password = MaskLine();
                        currentPlayer = Players.FirstOrDefault(r => r.Password == password && r.Name == username);
                        LoggedIn = (currentPlayer != null);
                        if (!LoggedIn) {
                            Console.WriteLine("{0}Invalid combination, please try again.", Environment.NewLine);
                        }
                    }
                    Console.WriteLine("{0}Please choose your colony.", Environment.NewLine);
                    foreach (Colony col in currentPlayer.Colonies) {
                        Console.WriteLine(@"{0}{1}:{0}{2}{0}Level {3}", Environment.NewLine, col.ID, col.Name, col.Level);
                    }
                    Console.WriteLine();
                    int colID = -1;
                    string colStr = Console.ReadLine();
                    while (!int.TryParse(colStr, out colID) || !currentPlayer.Colonies.Any(e => e.ID == colID)) {
                        if (colStr.ToLower() == "q") {
                            Console.WriteLine("Goodbye!");
                            return;
                        }
                        Console.WriteLine("{0}Invalid Colony. Please try again.", Environment.NewLine);
                        colStr = Console.ReadLine();
                    }
                    Console.WriteLine("Please enter a new name for the colony:");
                    string colName = Console.ReadLine();
                    bool b = IAPE.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified).Any();
                    currentPlayer.Colonies.First(e => e.ID == colID).Name = colName;
                    int change = IAPE.SaveChanges();
                    if (change >= 1) {
                        Console.WriteLine("{0}Colony has been renamed.", Environment.NewLine);
                    } else {
                        Console.WriteLine("{0}Colony could not be renamed.", Environment.NewLine);
                    }
                } else {
                    Console.WriteLine("No Registered users");
                }
            }

Ignore the über secure login, it's only in there to get a player entity from it for testing.

the bool b is set to true before the save, and then if I use the debugger to run the same line in the "Immediate Window" pane then it shows false. Which to me, means it saved? And If I re-run the application after it's closed then the changed value is in place. But eventually it will return to the previous value, and if I check the database straight after it still shows the original name, so I'm not quite sure what's going on here?

It's running on VCS2010, so Framework 4.

Thanks, Psy

A: 

IAPE.Savechanges() at some point.

Phil winkel
A: 

Checked the SQL going over to the database, and it's fine, after some investigation, turned out that I had a copy of the database within the project, which was over-riding the one in the output everytime I rebuilt it.

Psytronic