加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 更新条目时发生错误.有关详细信息,请参阅内部异常 – Linq

发布时间:2020-12-15 04:25:04 所属栏目:百科 来源:网络整理
导读:一直在环顾四周,我似乎无法弄清楚出了什么问题. 目前我正试图在玩家死亡时用新的高分来更新我的数据库.但无论我选择尝试和保存,它都会向我抛出异常. 码: HighScore hs = new HighScore(); var id = from i in db.HighScores orderby i.ID descending select
一直在环顾四周,我似乎无法弄清楚出了什么问题.

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

码:

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 MatematikSpilMenuSaveBunniesSceen.cs:line 173
       at MatematikSpilMenu.SaveBunniesSceen.Update(GameTime gameTime,Boolean otherScreenIsActive,Boolean coveredByOtherScreens) in C:UsersEtarnalazure-Aliendocumentsvisual studio 2010ProjectsMatematikSpilMenuMatematikSpilMenuMatematikSpilMenuSaveBunniesSceen.cs:line 110
       at MatematikSpilMenu.ScreenManager.Update(GameTime gameTime) in MatematikSpilMenuScreenManager.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 MatematikSpilMenuGame1.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,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:

解决方法

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

Server-generated keys and server-generated values are not supported by
SQL Server Compact.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读