SQL Server CE插入异常

本文关键字:插入异常 CE Server SQL | 更新日期: 2023-09-27 17:49:53

我试图在我的表Cities中插入新记录。它只有id (identity=true)name列。我使用实体框架工作:

public partial class Form1 : Form
{
    Database1Entities ent;
    public Form1()
    {
        InitializeComponent();
    }
    private void bSave_Click(object sender, EventArgs e)
    {
        ent.SaveChanges();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        ent = new Database1Entities();
        dataGridView1.DataSource = ent.Cities;
        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.Columns.Clear();
        DataGridViewTextBoxColumn colName = new DataGridViewTextBoxColumn();
        colName.Name = "Name";
        colName.DataPropertyName = "name";
        dataGridView1.Columns.Add(colName);
    }

当我编辑(或删除)一些现有信息并保存更改时,所有工作都很好,但当我试图添加新记录然后保存时,我收到异常:

 An unhandled exception of type 'System.Data.UpdateException' occurred in System.Data.Entity.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>WindowsFormsApplication2.vshost.exe</AppDomain><Exception><ExceptionType>System.Data.UpdateException, System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message> error on Updating record. For details, see the inner exception.</Message><StackTrace>   in System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   in System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
   in System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
   in System.Data.Objects.ObjectContext.SaveChanges()
   in WindowsFormsApplication2.Form1.button1_Click(Object sender, EventArgs e) in C:'Users'Misha'Documents'Visual Studio 2010'Projects'Испытательный полигон .NET'WindowsFormsApplication2'Form1.cs:строка 22
   in System.Windows.Forms.Control.OnClick(EventArgs e)
   in System.Windows.Forms.Button.OnClick(EventArgs e)
   in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   in System.Windows.Forms.Control.WmMouseUp(Message&amp;amp; m, MouseButtons button, Int32 clicks)
   in System.Windows.Forms.Control.WndProc(Message&amp;amp; m)
   in System.Windows.Forms.ButtonBase.WndProc(Message&amp;amp; m)
   in System.Windows.Forms.Button.WndProc(Message&amp;amp; m)
   in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m)
   in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m)
   in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;amp; msg)
   in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   in System.Windows.Forms.Application.Run(Form mainForm)
   in WindowsFormsApplication2.Program.Main() in C:'Users'Misha'Documents'Visual Studio 2010'Projects'Испытательный полигон .NET'WindowsFormsApplication2'Program.cs:строка 18
   in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   in System.Threading.ThreadHelper.ThreadStart()</StackTrace><ExceptionString>System.Data.UpdateException: Error on Updating record. For details, see the inner exception. ---&amp;gt; System.Data.EntityCommandCompilationException: При подготовке определения команды произошла ошибка. Подробные сведения см. во внутреннем исключении. ---&amp;gt; System.NotSupportedException: Ключи и значения, формируемые сервером, не поддерживаются SQL Server Compact.
   in System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateReturningSql(StringBuilder commandText, DbModificationCommandTree tree, ExpressionTranslator translator, DbExpression returning)
   in System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree tree, List`1&amp;amp; parameters, Boolean isLocalProvider)
   in System.Data.SqlServerCe.SqlGen.SqlGenerator.GenerateSql(DbCommandTree tree, List`1&amp;amp; parameters, CommandType&amp;amp; commandType, Boolean isLocalProvider)
   in System.Data.SqlServerCe.SqlCeProviderServices.CreateCommand(DbProviderManifest providerManifest, DbCommandTree commandTree)
   in System.Data.SqlServerCe.SqlCeProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
   in System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
   in System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
   in System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
   --- End of inner exception stack trace ---
   in System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
   in System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
   in System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
   in System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   --- End of inner exception stack trace ---
   in System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   in System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
   in System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
   in System.Data.Objects.ObjectContext.SaveChanges()
   in WindowsFormsApplication2.Form1.button1_Click(Object sender, EventArgs e) in C:'Users'Misha'Documents'Visual Studio 2010'Projects'Испытательный полигон .NET'WindowsFormsApplication2'Form1.cs:строка 22
   in System.Windows.Forms.Control.OnClick(EventArgs e)
   in System.Windows.Forms.Button.OnClick(EventArgs e)
   in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   in System.Windows.Forms.Control.WmMouseUp(Message&amp;amp; m, MouseButtons button, Int32 clicks)
   in System.Windows.Forms.Control.WndProc(Message&amp;amp; m)
   in System.Windows.Forms.ButtonBase.WndProc(Message&amp;amp; m)
   in System.Windows.Forms.Button.WndProc(Message&amp;amp; m)
   in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m)
   in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m)
   in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;amp; msg)
   in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   in System.Windows.Forms.Application.Run(Form mainForm)
   in WindowsFormsApplication2.Program.Main() in C:'Users'Misha'Documents'Visual Studio 2010'Projects'Испытательный полигон .NET'WindowsFormsApplication2'Program.cs:строка 18
   in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   in System.Threading.ThreadHelper.ThreadStart()</ExceptionString><InnerException><ExceptionType>System.Data.EntityCommandCompilationException, System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>При подготовке определения команды произошла ошибка. Подробные сведения см. во внутреннем исключении.</Message><StackTrace>   in System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
   in System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
   in System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
   in System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)</StackTrace><ExceptionString>System.Data.EntityCommandCompilationException: При подготовке определения команды произошла ошибка. Подробные сведения см. во внутреннем исключении. ---&amp;gt; System.NotSupportedException: Ключи и значения, формируемые сервером, не поддерживаются SQL Server Compact.
   in System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateReturningSql(StringBuilder commandText, DbModificationCommandTree tree, ExpressionTranslator translator, DbExpression returning)
   in System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree tree, List`1&amp;amp; parameters, Boolean isLocalProvider)
   in System.Data.SqlServerCe.SqlGen.SqlGenerator.GenerateSql(DbCommandTree tree, List`1&amp;amp; parameters, CommandType&amp;amp; commandType, Boolean isLocalProvider)
   in System.Data.SqlServerCe.SqlCeProviderServices.CreateCommand(DbProviderManifest providerManifest, DbCommandTree commandTree)
   in System.Data.SqlServerCe.SqlCeProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
   in System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
   in System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
   in System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
   --- End of inner exception stack trace ---
   in System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
   in System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
   in System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
   in System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)</ExceptionString><InnerException><ExceptionType>System.NotSupportedException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Ключи и значения, формируемые сервером, не поддерживаются SQL Server Compact.</Message><StackTrace>   in System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateReturningSql(StringBuilder commandText, DbModificationCommandTree tree, ExpressionTranslator translator, DbExpression returning)
   in System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree tree, List`1&amp;amp; parameters, Boolean isLocalProvider)
   in System.Data.SqlServerCe.SqlGen.SqlGenerator.GenerateSql(DbCommandTree tree, List`1&amp;amp; parameters, CommandType&amp;amp; commandType, Boolean isLocalProvider)
   in System.Data.SqlServerCe.SqlCeProviderServices.CreateCommand(DbProviderManifest providerManifest, DbCommandTree commandTree)
   in System.Data.SqlServerCe.SqlCeProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
   in System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
   in System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
   in System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)</StackTrace><ExceptionString>System.NotSupportedException: Ключи и значения, формируемые сервером, не поддерживаются SQL Server Compact.
   in System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateReturningSql(StringBuilder commandText, DbModificationCommandTree tree, ExpressionTranslator translator, DbExpression returning)
   in System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree tree, List`1&amp;amp; parameters, Boolean isLocalProvider)
   in System.Data.SqlServerCe.SqlGen.SqlGenerator.GenerateSql(DbCommandTree tree, List`1&amp;amp; parameters, CommandType&amp;amp; commandType, Boolean isLocalProvider)
   in System.Data.SqlServerCe.SqlCeProviderServices.CreateCommand(DbProviderManifest providerManifest, DbCommandTree commandTree)
   in System.Data.SqlServerCe.SqlCeProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
   in System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
   in System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
   in System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)</ExceptionString></InnerException></InnerException></Exception></TraceRecord>
An unhandled exception of type 'System.Data.UpdateException' occurred in System.Data.Entity.dll
最有趣的是,如果我使用MySQL数据库,一切都很好。相同的表,相同的自动生成模型。

SQL Server CE插入异常

您使用的SQL Compact 3.5不支持标识列,EF也不支持SQL CE 3.5。使用NuGet获取软件包EntityFramework.SqlServerCompact,它将拉下SQL CE 4.0 SP1和支持SQL CE 4.0的EF 4.3.1。