实体框架未实际保存更改 [英] Entity Framework not actually saving changes

查看:79
本文介绍了实体框架未实际保存更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始涉足EF,而且我正在努力解决它(通常很容易)。但是出于某种原因,它实际上并没有保存结果,尽管它似乎可能会缓存一段时间,然后回滚到以前的值?

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");
                }
            }

忽略über安全登录,它只是在那里

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

在保存之前,将bool b设置为true,然后使用调试器在立即窗口窗格,然后显示false。对我来说,这意味着它可以保存?而且,如果我在关闭应用程序后重新运行它,那么更改后的值就位了。但是最终它会返回到先前的值,如果我在仍然显示原始名称后立即检查数据库,那么我不确定这里发生了什么事情。

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?

它在VCS2010上运行,因此是Framework 4。

It's running on VCS2010, so Framework 4.

谢谢,Psy

推荐答案

检查了传递到数据库的SQL,经过一些调查,结果发现我在项目中有一个数据库副本,每次重建时它都会覆盖输出中的一个副本。

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.

这篇关于实体框架未实际保存更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆