【问题标题】:An error occurred while updating the entries. See the inner exception for details. - Linq to Entity更新条目时出错。有关详细信息,请参阅内部异常。 - Linq 到实体
【发布时间】:2012-06-08 11:23:48
【问题描述】:

一直在环顾四周,我似乎无法弄清楚出了什么问题。

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

代码:

 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?外键?有没有 ID 1 的游戏?
  • GameID 是我用来为相应游戏提取相应高分的 ID。 GameID = 1 用于告诉游戏应该在哪里拉出哪个高分。
  • 但我会掌握内部异常
  • 您是否检查过 HIghScore 表是否为空?
  • Kamal,抱歉,我不明白你在说什么 =/ 你能改写一下吗?

标签: c# database xna linq-to-entities


【解决方案1】:

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

【讨论】:

  • 感谢您让我想到触发器。我已经走到了尽头。
【解决方案2】:

在我看来,问题的根源在于您使用的 SQL 服务器:

不支持服务器生成的键和服务器生成的值 SQL Server Compact。

您是否尝试在 SQL Server Compact 版本上使用自动增量 ID?我记不太清了,但也许用那个 SQL 版本是不可能的。我建议你先检查一下。

【讨论】:

  • 问题是,它以前在表单上工作过。除非两者之间有区别?
  • 我不太明白你的意思。你的意思是相同的代码之前运行过一次?如果是这样,那么您必须检查自那以后您发生了什么变化。从异常消息看来,问题出在数据库上。你改了后面的数据库吗?
  • 刚刚尝试在 ID 上删除 Auto Increment 关闭,仍然给了我同样的错误。
  • 难道你的 'db.HighScores' 对象仍然试图将其作为身份处理吗?
  • 我不这么认为,因为我不得不删除它并重新插入它,因为它一直给我错误,截图有帮助吗?
【解决方案3】:

找到了解决方案,似乎它的数据库在使用太多地方时可能会出错(这只是我的猜测)。无论哪种方式,创建一个新表然后向其中添加东西似乎都可以解决它(没有使用身份。)

【讨论】:

    【解决方案4】:

    在我的情况下,在打开表数据库后,auto_ink 列的主键问题就消失了。

    【讨论】:

      【解决方案5】:

      服务器生成的密钥表现不佳所以去检查数据库可能是你的数据库初级密钥是自动生成的密钥所以去尝试手动插入数据

      【讨论】:

        猜你喜欢
        • 2013-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多