c# - RemoveRange 上的 DbUpdate 异常
问题描述
我正在尝试清除一张桌子,但是,当我这样做时,我得到了一个DbUpdateException
. 我不确定是什么原因造成的,因为没有外键约束。
调用代码如下
/* Get the context */
NLISdBContext databaseContext = contextFactory.GetNLISdBContext();
/* Remove the data */
databaseContext.Picexceptions.RemoveRange(databaseContext.Picexceptions);
databaseContext.SaveChanges(); // <-- Error Here
可能的原因
桌子上有一个Contraint,这可能是导致它的原因,但我不确定为什么会这样。见约束:
CONSTRAINT [filteredPIC_ProgramCode_ExceptionStatus_IDX] PRIMARY KEY CLUSTERED
(
[PIC] ASC,
[ProgramCode] ASC,
[ExceptionStatus] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
我的理由是,如果删除了一行,它不应该违反约束。会不会是如果删除所有这些会导致集群索引出现问题?
如果不是这样,此异常的其他常见原因是什么?
例外
NLISLib.DataSources.NLISdBContext_PrimaryDataSource Encountered exception in PurgePICExceptions: Microsoft.EntityFrameworkCore.DbUpdateException:
An error occurred while updating the entries. See the inner exception for details. ---> Microsoft.Data.SqlClient.SqlException: Internal Query Processor Error: The query processor encountered an unexpected error during execution (HRESULT = 0x80040e19).
at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows)
at Microsoft.Data.SqlClient.SqlDataReader.TryHasMoreResults(Boolean& moreResults)
at Microsoft.Data.SqlClient.SqlDataReader.TryNextResult(Boolean& more)
at Microsoft.Data.SqlClient.SqlDataReader.NextResult()
at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(RelationalDataReader reader)
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(RelationalDataReader reader)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList`1 entries)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1 entriesToSave)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(DbContext _, Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
at NLISLib.DataSources.NLISdBContext_PrimaryDataSource.PurgePICExceptions()
解决方案
试试这个
var removedItems=databaseContext.Picexceptions.ToArray();
databaseContext.Picexceptions.RemoveRange(removedItems);
databaseContext.SaveChanges();
但是有些事情告诉我,如果 db 有很多记录,这会更有效率
context.Database.ExecuteSqlRaw("TRUNCATE TABLE [Picexceptions]");
或者您可以尝试 Database.ExecuteSqlCommand。这取决于版本。
推荐阅读
- python - 如何在python中填充轮廓
- javascript - 如何从 post.ejs 获取输入值
- python - RaspberryPi 4 Python 使用 SIM900 发送彩信
- c# - 授权属性不适用于 IdentityServer4 和 .NET Core 3.1
- amazon-web-services - 使用 AWS Rekognition 或 Google Vision API 对位模糊文本进行 OCR
- spring - 如何在 Spring Web Service 请求中删除不需要的命名空间并更改命名空间位置
- selenium - 尝试为 MMT 站点执行此代码时出错
- javascript - 班级组合不应用新风格
- bash - 用 IP 地址替换参数
- excel - Excel Vlookup 公式疑难解答