Utilizzo di Entity Framework 4.3.1 Database In primo luogo, quale è un buon metodo per salvare/salvare frequentemente le modifiche agli oggetti nel database? Nel seguito, vorrei salvare la fattura immediatamente dopo la chiamata ai quickbook e non rischiare di attendere che tutte le fatture vengano pubblicate. Ma, non posso chiamare SaveChanges ogni volta nel ciclo, genererà un'eccezione.Salvataggi frequenti con il framework entità
Sarebbe utile avere un metodo .Save() su ciascun oggetto, forse c'è un buon modo per farlo?
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();
Questa è l'eccezione generata quando si chiama SaveChanges() nel ciclo.
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)
Come fallisce quando si chiama 'SaveChanges' all'interno del ciclo? –
Ho aggiunto l'eccezione alla domanda. – RyanW