使用 C# EF6 保存时传递给系统调用的数据区域太小

The data area passed to a system call is too small when saving with C# EF6

我有一个 C#、Winforms EF6 应用程序。数据库是 SQL Server Compact 4。 在一些计算机中(大多数工作正常)我在保存一个简单的对象时得到异常 "The data area passed to a system call is too small",如下面的代码

        public void Add(SimpleObject simpleObject)
        {
            try
            {
                using (var db = new MyDbContext())
                {
                    db.SimpleObjects.AddOrUpdate(e => e.Description, simpleObject);
                    db.SaveChanges();
                }
            }
            catch(Exception ex)
            {
                _logger.ErrorException("Add", ex);
                throw;
            }
        }

SimpleObject 非常简单,只有 2 列([ID] INT NOT NULL IDENTITY (1,1),[DESCRIPTION] NVARCHAR(4000))。抛出异常的实例在大多数计算机中通常保存的描述字段中具有通用文本(低于 150 个字符)。命令 AddOrUpdate 抛出异常。

使用 NLog 记录的完整异常详细信息:

System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlServerCe.SqlCeException: The data area passed to a system call is too small. at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr) at System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan() at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) at System.Data.Entity.SqlServerCompact.SqlCeMultiCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func3 operation, TInterceptionContext interceptionContext, Action3 executing, Action3 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) --- End of inner exception stack trace --- at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues) at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClass7.b__5() at System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator1.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source) at System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate[TEntity](DbSet1 set, IEnumerable1 identifyingProperties, InternalSet1 internalSet, TEntity[] entities) at System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate[TEntity](IDbSet1 set, Expression1 identifierExpression, TEntity[] entities)>

非常感谢任何帮助!

您应该升级到 SQL CE 4.0 SP1,因为它包含针对该问题的修复程序 - 请参阅 http://erikej.blogspot.com/2012/03/sql-server-compact-40-sp1-ctp1.html