为什么 DbContextctx
在每次SaveChanges()
执行后处理得更快?
第一个样本:
var ctx = new DomainContext();
foreach (var item in deals)
{
DealsOfReutersAddition newDealAddition =
new DealsOfReutersAddition
{
DealsOfReutersId = item.DealsOfReutersId,
DealDate = DateTime.Parse(item.DateConfirmed ?? "01 01 01", new CultureInfo("en-US")),
};
ctx.DealsOfReutersAdditions.Add(newDealAddition);
ctx.SaveChanges();
}
ctx.Dispose();
第二个样本:
foreach (var item in deals)
{
DealsOfReutersAddition newDealAddition =
new DealsOfReutersAddition
{
DealsOfReutersId = item.DealsOfReutersId,
DealDate = DateTime.Parse(item.DateConfirmed ?? "01 01 01", new CultureInfo("en-US")),
};
var ctx = new DomainContext();
ctx.DealsOfReutersAdditions.Add(newDealAddition);
ctx.SaveChanges();
ctx.Dispose();
}
在 1000 行上,第二个样本估计为 8 秒,而第一个样本为 140 秒。有什么方法可以清理 ctx 而不是重新创建?