我发现了一个简单的方法来做到这一点:
// We will need a quotation Id (see below). Make sure we have one
SaveQuotation(myQuotation);
// Read the large claim history
var claimCollection = ImportClaimsFromExcelFile(fileName);
// Save the claim history without using EF. The quotation Id is needed to
// link the history to the quotation.
SaveClaimCollectionUsingSqlBulkCopy(claimCollection, myQuotation.Id);
// Now, ask EF to reload the quotation.
LoadQuotation(myQuotation.Id);
拥有60个000索赔,这个代码在10秒运行的历史。使用myObjectContext.SaveChanges()
,10分钟还不够...
感谢您的建议!
注:这里是我以前大部分的代码插入要求:
using (var connection = new SqlConnection(constring))
{
connection.Open();
using (var copy = new SqlBulkCopy(connection))
{
copy.DestinationTableName = "ImportedLoss";
copy.ColumnMappings.Add("ImporterId", "ImporterId");
copy.ColumnMappings.Add("Loss", "Loss");
copy.ColumnMappings.Add("YearOfLoss", "YearOfLoss");
copy.BatchSize = 1000;
copy.WriteToServer(dt);
}
connection.Close();
}
来源
2013-05-22 15:38:12
SRO
这是更多钞票来尝试存储过程的方法吗? – Jonathan
感谢您的建议。但据我所知,我将不得不使用存储过程保存整个对象。正如Giorgio指出的那样,我会放弃使用ORM的优势... – SRO