c# - 更新条目时出错。有关详细信息,请参阅内部异常。 - Linq 到实体

标签 c# database xna linq-to-entities

环顾四周,我似乎无法弄清楚哪里出了问题。

目前我正在尝试在玩家死亡时用新的高分更新我的数据库。但它一直向我抛出异常,无论我选择尝试和保存什么。

代码:

 HighScore hs = new HighScore();
            var id = from i in db.HighScores
                     orderby i.ID descending
                     select i;
            int newId = 0;

            if (id.Count() == 0)
            {
                newId = 1;
            }
            else
            {
                newId = id.First().ID + 1;
            }
        hs.ID = 6; //I just hardcoded in 6 to make sure i wasent because of the newId      //thing, and i have checked if theres already something on the sixths spot as well.
            hs.UserHighscore = 100;
            hs.HighscoreUsername = "test";
            hs.GameID = 1;
            db.HighScores.AddObject(hs);
            db.SaveChanges();

我检查了一遍又一遍,但我似乎无法弄清楚问题出在哪里。

如有任何帮助,我们将不胜感激。

异常:

System.Data.UpdateException was unhandled
  Message=An error occurred while updating the entries. See the inner exception for details.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
       at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
       at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
       at System.Data.Objects.ObjectContext.SaveChanges()
       at MatematikSpilMenu.SaveBunniesSceen.SaveHighscore() in MatematikSpilMenu\SaveBunniesSceen.cs:line 173
       at MatematikSpilMenu.SaveBunniesSceen.Update(GameTime gameTime, Boolean otherScreenIsActive, Boolean coveredByOtherScreens) in C:\Users\Etarnalazure-Alien\documents\visual studio 2010\Projects\MatematikSpilMenu\MatematikSpilMenu\MatematikSpilMenu\SaveBunniesSceen.cs:line 110
       at MatematikSpilMenu.ScreenManager.Update(GameTime gameTime) in MatematikSpilMenu\ScreenManager.cs:line 101
       at Microsoft.Xna.Framework.Game.Update(GameTime gameTime)
       at Microsoft.Xna.Framework.Game.Tick()
       at Microsoft.Xna.Framework.Game.HostIdle(Object sender, EventArgs e)
       at Microsoft.Xna.Framework.GameHost.OnIdle()
       at Microsoft.Xna.Framework.WindowsGameHost.RunOneFrame()
       at Microsoft.Xna.Framework.WindowsGameHost.ApplicationIdle(Object sender, EventArgs e)
       at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(Int32 grfidlef)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Microsoft.Xna.Framework.WindowsGameHost.Run()
       at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
       at Microsoft.Xna.Framework.Game.Run()
       at MatematikSpilMenu.Program.Main() in MatematikSpilMenu\Game1.cs:line 120
  InnerException: System.Data.EntityCommandCompilationException
       Message=An error occurred while preparing the command definition. See the inner exception for details.
       Source=System.Data.Entity
       StackTrace:
            at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
            at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
            at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
            at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
       InnerException: System.NotSupportedException
            Message=Server-generated keys and server-generated values are not supported by SQL Server Compact.
            Source=System.Data.SqlServerCe.Entity
            StackTrace:
                 at System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateReturningSql(StringBuilder commandText, DbModificationCommandTree tree, ExpressionTranslator translator, DbExpression returning)
                 at System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree tree, List`1& parameters, Boolean isLocalProvider)
                 at System.Data.SqlServerCe.SqlGen.SqlGenerator.GenerateSql(DbCommandTree tree, List`1& parameters, CommandType& commandType, Boolean isLocalProvider)
                 at System.Data.SqlServerCe.SqlCeProviderServices.CreateCommand(DbProviderManifest providerManifest, DbCommandTree commandTree)
                 at System.Data.SqlServerCe.SqlCeProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
                 at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
                 at System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
                 at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
            InnerException: 

最佳答案

根据我的经验,这通常是由数据库错误触发的,在我的例子中,它是与失败的表关联的触发器并抛出根异常。但是,在您的情况下,您似乎在使用自动编号(身份)字段时错误地尝试手动定义 ID。

关于c# - 更新条目时出错。有关详细信息,请参阅内部异常。 - Linq 到实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10947892/

相关文章:

mysql - 与一列的 10 个唯一记录不同

mysql - 插入从内部联接查询派生的数据时出错

c# - AI 敌人在追逐玩家时会聚在一起

c# - 为什么 Environment.CommandLine.Trim ('"') 不删除尾随引号?

c# - 传递给函数的条件语句

javascript - 自动生成文档的随机测试数据以播种 MongoDB 数据库

c# - 为什么要使用服务(IServiceProvider)?

c# - .Net 安装项目/升级和维护单独的版本化文件夹

c# - httprepl : Cannot start the REPL when output is being redirected

c# - XNA - 使用动态顶点缓冲区和只有四 (4) 个顶点