I have an application using LINQ-to-SQL with SQL Server CE 4. I know that this isn't officially supported, but we have made it work, with the following exception. On occasion, we get an error with a row in the database when trying to update it.
We use a DataContext
to select a row from the database, update a few columns (a boolean and a byte column), then we call SubmitChanges()
. When we submit the changes, we are getting a DivideByZeroException with the following stack trace:
System.DivideByZeroException: Attempted to divide by zero.
at System.Data.SqlServerCe.NativeMethodsHelper.CompileQueryPlan(IntPtr pQpCommand, String pwszCommandText, ResultSetOptions options, IntPtr[] pParamNames, IntPtr prgBinding, Int32 cDbBinding, IntPtr& pQpPlan, IntPtr pError)
at System.Data.SqlServerCe.NativeMethods.CompileQueryPlan(IntPtr pQpCommand, String pwszCommandText, ResultSetOptions options, IntPtr[] pParamNames, IntPtr prgBinding, Int32 cDbBinding, IntPtr& pQpPlan, IntPtr pError)
at System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()
at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery()
at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicUpdate(TrackedObject item)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.Update(TrackedObject item)
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
EDIT:
Upon profiling, the problem appears to be generated on a delete statement, similar to this:
DELETE FROM [WorkItemUid] WHERE ([Oid] = 24151 /* @p0 /) AND ([WorkItemOid] = 745 / @p1 /) AND ([SeriesInstanceUid] = '1.3.12.2.1107.5.1.4.54023.30000004101914490887500000063' / @p2 /) AND ([SopInstanceUid] = '1.3.12.2.1107.5.1.4.54023.30000004101913521221800001089' / @p3 /) AND ([Complete] = 1) AND ([FailureCount] = 0 / @p4 */) AND ([File] IS NULL) AND (NOT ([Failed] = 1))
The error was happening in a statement where I was attempting to individually delete ~1500 foreign key records. When I changed the code to delete rows individually, I narrowed it down to about 45 rows that couldn't be deleted. I then used SQL Compact Query Analyzer to attempt to execute the delete on the lines themselves, and it did fail there also, with the divide by zero exception.
So, it appears to not be a Linq-to-Sql error, but rather something with SQL Server Compact CE 4.0 itself deleting these rows. I've attempted to delete and start with a clean database, and replicated the populating and then deleting from the database, and the problem happens again. It might be an issue when there's > 1500 foreign key records that something happens when deleting.
A bunch of google searches were mostly empty, but I did find a reference to a DivideByZeroException
with SQL Server CE 3.5 at this reference. I have confirmed I'm using the SQL Server CE 4.0 assemblies, so this does not seem to be the issue. Any ideas on if the above bug still applies to SQL Server CE 4.0? It appears after further review it is the same problem.
Upgrading to SQL Server Compact CTP1 appears to have resolved the problem. I am no longer seeing the exceptions when deleting rows as reported in the question. The problem appeared to be related to deleting rows from a table where there were > 1500 rows in a foreign key relationship.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With