Insert occurs exception SQL Server CE

I was trying to insert new record in my table Cities. It has only id (identity=true) and name columns. I work using Entity Framework:

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

When I edit(or delete) some existing info and save changes all works good, but when I tried to add new record and then save I received exception:

 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

The most interersting is that if I use MySQL database all works fine. Same table, same auto-generated model.

Upvotes: 0

Views: 728

Answers (1)

Allon Guralnek
Allon Guralnek

Reputation: 16121

You are using SQL Compact 3.5 which doesn't support identity columns, nor does EF support SQL CE 3.5. Use NuGet to retrieve the package EntityFramework.SqlServerCompact which will pull down both SQL CE 4.0 SP1 and EF 4.3.1 with SQL CE 4.0 support.

Upvotes: 1

Related Questions