我刚刚开始涉足EF,我正试图绕过它(通常很容易)。但是由于某种原因,它实际上并没有保存结果,虽然它似乎可以缓存一段时间的值,然后回滚到之前的值?
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安全登录,只有在那里才能从中获取一个玩家实体进行测试。
在保存之前将bool b设置为true,然后如果我使用调试器在“立即窗口”窗格中运行相同的行,则它显示为false。对我来说,意味着它得救?如果我在应用程序关闭后重新运行应用程序,那么更改的值就会到位。但最终它将返回到之前的值,如果我在显示原始名称后直接检查数据库,那么我不太确定这里发生了什么?
它在VCS2010上运行,因此框架4。
谢谢,Psy
答案 0 :(得分:2)
检查SQL转到数据库,经过一些调查后发现我在项目中有一个数据库副本,每次我重建它时都会覆盖输出中的数据库。 / p>
答案 1 :(得分:0)
IAPE.Savechanges()在某些时候。