簡體   English   中英

插入發生異常SQL Server CE

[英]Insert occurs exception SQL Server CE

我試圖在表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 Compact 3.5不支持標識列,EF也不支持SQL CE 3.5。 使用NuGet來檢索EntityFramework.SqlServerCompact軟件包,該軟件包將下拉具有SQL CE 4.0支持的SQL CE 4.0 SP1和EF 4.3.1。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM