Entity Framework Foreach 循环中的事务
Entity Framework Transactions in Foreach Loop
我正在尝试使用 Entity framework 从 Windows Forms DataGridView 中保存数据。由于某种原因,记录不会保存到数据库中。我想遍历 DataGridView 并将每一行添加到一个事务对象中,并将所有事务(行)一次保存到数据库中。
我在交易中找到的信息来自 MSDN. While searching for a solution to the problem I came across this SO post,其中有一条评论显示 "I'm taking an exception to trapping the exception like this. It causes the database operation to fail silently." 这似乎是我的代码中发生的事情。我没有收到任何错误,它只是没有将任何内容保存到数据库中。
如何自动保存所有事务并在出现故障时回滚?我如何重新组织事物以正确捕获错误?如果注释正确,MSDN 的代码有什么问题?
这是我的代码:
// Store the valid transactions
using ( var dbContextTransaction = context.Database.BeginTransaction() )
{
try
{
Transaction oTransaction = new Transaction();
foreach ( DataGridViewRow row in dgvInventoryTransactions.Rows )
{
if ( !row.IsNewRow )
{
// Get the product
int iSelectedProduct = int.Parse( row.Cells[0].Value.ToString() );
oTransaction.Product = context.Products.First( p => p.ID == iSelectedProduct );
// Quantities
oTransaction.FullQuantity = int.Parse( row.Cells[1].Value.ToString() );
oTransaction.PartialQuantity = int.Parse( row.Cells[2].Value.ToString() );
oTransaction.CalculatedQuantity = (double)oTransaction.FullQuantity + ( (double)oTransaction.PartialQuantity / (double)oTransaction.Product.Pieces );
// Check to see if a truck has been selected
if ( !string.IsNullOrEmpty( row.Cells[3].FormattedValue as String ) )
{
// A truck has been selected
int iSelectedVehicle;
if ( int.TryParse( row.Cells[3].FormattedValue.ToString(), out iSelectedVehicle ) )
{
oTransaction.Vehicle = context.Vehicles.Where( v => v.VehicleNumber == iSelectedVehicle ).First();
}
}
// Check to see if the "Source" field contains data
if ( !string.IsNullOrEmpty( row.Cells[4].FormattedValue as String ) )
{
oTransaction.SourceNumber = row.Cells[4].Value.ToString();
}
// Check to see if the comments field contains data
if ( !string.IsNullOrEmpty( row.Cells[5].FormattedValue as String ) )
{
oTransaction.Comments = row.Cells[5].Value.ToString();
}
// Get the transaction type
int value = int.Parse( cmboTransactionType.SelectedValue.ToString() );
oTransaction.TransactionType = context.TransactionTypes.Where( t => t.ID == value ).First();
// Get the current user
var user = context.Employees.First( u => u.ID == Globals.User ); // One lookup...
oTransaction.CreatedBy = user;
oTransaction.LastUpdatedBy = user;
// Add the dates
oTransaction.TransactionDate = dtpTransactionDate.Value;
oTransaction.CreateDate = DateTime.Now;
oTransaction.LastUpdatedDate = DateTime.Now;
// Save the transaction
context.SaveChanges();
}
}
// Commit the changes to the database
dbContextTransaction.Commit();
}
catch ( Exception )
{
dbContextTransaction.Rollback();
MessageBox.Show( "Records were NOT saved successfully.", "Save Unsuccessful", MessageBoxButtons.OK, MessageBoxIcon.Information );
}
}
您尚未插入 oTransaction
或将其连接到某个现有对象。 EF 不知道这个对象存在。尝试使用 AddObject
方法添加交易。这会将其注册到 EF.
此外,您可以删除 Rollback
调用。它在没有提交时回滚。也将消息框移出using块以快速结束事务。
我正在尝试使用 Entity framework 从 Windows Forms DataGridView 中保存数据。由于某种原因,记录不会保存到数据库中。我想遍历 DataGridView 并将每一行添加到一个事务对象中,并将所有事务(行)一次保存到数据库中。
我在交易中找到的信息来自 MSDN. While searching for a solution to the problem I came across this SO post,其中有一条评论显示 "I'm taking an exception to trapping the exception like this. It causes the database operation to fail silently." 这似乎是我的代码中发生的事情。我没有收到任何错误,它只是没有将任何内容保存到数据库中。
如何自动保存所有事务并在出现故障时回滚?我如何重新组织事物以正确捕获错误?如果注释正确,MSDN 的代码有什么问题?
这是我的代码:
// Store the valid transactions
using ( var dbContextTransaction = context.Database.BeginTransaction() )
{
try
{
Transaction oTransaction = new Transaction();
foreach ( DataGridViewRow row in dgvInventoryTransactions.Rows )
{
if ( !row.IsNewRow )
{
// Get the product
int iSelectedProduct = int.Parse( row.Cells[0].Value.ToString() );
oTransaction.Product = context.Products.First( p => p.ID == iSelectedProduct );
// Quantities
oTransaction.FullQuantity = int.Parse( row.Cells[1].Value.ToString() );
oTransaction.PartialQuantity = int.Parse( row.Cells[2].Value.ToString() );
oTransaction.CalculatedQuantity = (double)oTransaction.FullQuantity + ( (double)oTransaction.PartialQuantity / (double)oTransaction.Product.Pieces );
// Check to see if a truck has been selected
if ( !string.IsNullOrEmpty( row.Cells[3].FormattedValue as String ) )
{
// A truck has been selected
int iSelectedVehicle;
if ( int.TryParse( row.Cells[3].FormattedValue.ToString(), out iSelectedVehicle ) )
{
oTransaction.Vehicle = context.Vehicles.Where( v => v.VehicleNumber == iSelectedVehicle ).First();
}
}
// Check to see if the "Source" field contains data
if ( !string.IsNullOrEmpty( row.Cells[4].FormattedValue as String ) )
{
oTransaction.SourceNumber = row.Cells[4].Value.ToString();
}
// Check to see if the comments field contains data
if ( !string.IsNullOrEmpty( row.Cells[5].FormattedValue as String ) )
{
oTransaction.Comments = row.Cells[5].Value.ToString();
}
// Get the transaction type
int value = int.Parse( cmboTransactionType.SelectedValue.ToString() );
oTransaction.TransactionType = context.TransactionTypes.Where( t => t.ID == value ).First();
// Get the current user
var user = context.Employees.First( u => u.ID == Globals.User ); // One lookup...
oTransaction.CreatedBy = user;
oTransaction.LastUpdatedBy = user;
// Add the dates
oTransaction.TransactionDate = dtpTransactionDate.Value;
oTransaction.CreateDate = DateTime.Now;
oTransaction.LastUpdatedDate = DateTime.Now;
// Save the transaction
context.SaveChanges();
}
}
// Commit the changes to the database
dbContextTransaction.Commit();
}
catch ( Exception )
{
dbContextTransaction.Rollback();
MessageBox.Show( "Records were NOT saved successfully.", "Save Unsuccessful", MessageBoxButtons.OK, MessageBoxIcon.Information );
}
}
您尚未插入 oTransaction
或将其连接到某个现有对象。 EF 不知道这个对象存在。尝试使用 AddObject
方法添加交易。这会将其注册到 EF.
此外,您可以删除 Rollback
调用。它在没有提交时回滚。也将消息框移出using块以快速结束事务。