我使用了插入代码 - 'shown down' - 将示例数据插入 MS SQL DB 。其中一个表 - '显示下来' - (插入'记录'的最后一个表)有很大的插入,在调试模式下需要 5 分钟,但是当我尝试在不调试的情况下启动项目时,数据库和 IIS-express 中的表是空的没有抛出错误
数据有超过 36,000 行和超过 40 列
我假设可能是因为“连接超时”
所以我试图寻找一个解决方案,发现两个都不起作用:
1.连接字符串
Persist Security Info=False;Connect Timeout=1000;
2.Database.SetCommandTimeout
public MainContext()
{
//base.Database.SetCommandTimeout(1000);
}
这里插入代码
修改自 https://github.com/aspnet/MusicStore/blob/dev/samples/MusicStore/Models/SampleData.cs
private static async Task AddOrUpdateAsync<TEntity>(
IServiceProvider serviceProvider,
Func<TEntity, object> propertyToMatch,
IEnumerable<TEntity> entities)
where TEntity : class
{
Exception exp = null;
using (var scope = serviceProvider
.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
using (var db = scope
.ServiceProvider
.GetService<MainContext>())
using (var transaction = db.Database.BeginTransaction())
{
try
{
db.ChangeTracker.AutoDetectChangesEnabled = false;
db.Set<TEntity>().AddRange(entities);
db.SaveChanges();
transaction.Commit();
}
catch (Exception ex)
{
exp = ex;
throw ex;
}
}
if (exp != null)
{
throw exp;
}
}