gpt4 book ai didi

.net - 使用 Entity Framework 频繁保存

转载 作者:行者123 更新时间:2023-12-04 14:16:40 28 4
gpt4 key购买 nike

使用 Entity Framework 4.3.1 数据库 首先,频繁提交/保存对象更改到数据库的好方法是什么?在下面,我想在调用 quickbooks 后立即保存发票,而不是冒着等待所有发票过帐的风险。但是,我不能在循环中每次都调用 SaveChanges,它会抛出异常。

在每个对象上都有一个 .Save() 方法会很方便,也许有一个很好的方法来做到这一点?

var unpostedInvoices = entities.GetUnpostedInvoices();
foreach (Invoice invoice in unpostedInvoices)
{
// this takes a long time
var invoiceDto = quickbooks.PostInvoice(invoice);

invoice.Posted = true;
invoice.TransactionId = invoiceDto.TransactionId;

// I'd like to save here rather than after the foreach loop, but this will fail
//entities.SaveChanges();
}

// this works, but I don't want to risk waiting this long to save
entities.SaveChanges();

这是在循环中调用 SaveChanges() 时抛出的异常。
New transaction is not allowed because there are other threads running in the session.

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName)
at System.Data.SqlClient.SqlInternalConnection.BeginTransaction(IsolationLevel iso)
at System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)
at System.Data.Common.DbConnection.BeginTransaction(IsolationLevel isolationLevel)
at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)

最佳答案

这个问题可能有帮助:https://stackoverflow.com/questions/2113498

当您仍在读取结果集和 SaveChanges 时,您无法开始新事务。如果一个交易不存在,则创建一个交易。

一种解决方案是先完成读取,然后遍历内存中的结果集。

如果您更改此行:

var unpostedInvoices = entities.GetUnpostedInvoices().ToList();

...你能把 SaveChanges回到循环内?

关于.net - 使用 Entity Framework 频繁保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10915400/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com