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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10947892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:54:11  来源:igfitidea点击:

An error occurred while updating the entries. See the inner exception for details. - Linq to Entity

c#databasexnalinq-to-entities

提问by Etarnalazure

been looking around and i just cant seem to figure out what is wrong.

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

Currently i'm trying to update my database with a new highscore when ever the player dies. But it keeps throwing that exception at me, no matter what i choose to try and save.

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

Code:

代码:

 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();

I've checked, again and again, and i just cant seem to figure out what the problem is.

我一遍又一遍地检查,我似乎无法弄清楚问题是什么。

Any help would be appriciated.

任何帮助将被appriciated。

The exception:

例外:

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: 

采纳答案by Csaba Benko

It seems to me that the root of the problems resides on the SQL server you use:

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

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

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

Are you trying to use an auto increment ID on SQL server Compact version? I'm not remember well, but maybe that is not possible with that SQL version. I suggest you to check that first.

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

回答by Etarnalazure

Found the solution, it seems that its the database that can bug if in use too many places (This is just me guessing). Either way, creating a new table and then adding stuff to there seemed to fix it (Did not use Identity.)

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

回答by Ricardo Camacho

Well from my experience that is usually triggered by a db error, in my case it was a trigger associated with the table that was failing and throwing the root exception. In your case however it seems that you are mistakenly trying to define the id manually when using an auto-numbered (identity) field.

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

回答by Massimo

In my case after switching on the table database the primary key to an auto_ink column the problem gone.

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

回答by Abrham

Server-generated keys are not performing well so go and check the database may be ur database primery key is auto generated key so go and try to insert data manually

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

回答by Rustam Irzaev

Also check relationships between tables.

还要检查表之间的关系。