我想从Azure上托管的应用程序中重新创建一个错误场景。
更新实体时(从excel导入一些文件时(出现错误:
System.Data.SqlClient.SqlException: Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
调用SaveChanges((时发生错误
我认为插入数据太大,这导致了一个问题。因此,我需要在本地重新创建它。为此,我想减少这个超时,但我做不到。我已经尝试过做的:
第一种方法:
public AppDbContext() : base("AppDb")
{
_logger = LogManager.GetCurrentClassLogger();
Database.CommandTimeout = 10; // normlany this is set to 180 but I want to avoid
large time for waiting for error so I set it to
10
}
第二种方法:
我更改了连接字符串
Data Source=.SQLEXPRESS;Initial Catalog=Application-DEV;Integrated Security=True;MultipleActiveResultSets=true;Connection Timeout=1
我也试着这样做:
using (var context = new AppDbContext())
{
context.Database.CommandTimeout = 2;
var breakPointOne = 1;
context.FunctionalLocations.AddRange(data);
context.SaveChanges();
var breakPointTwo = 2; // this breakpoint is reached after 50 seconds...
}
// why exception isn't throwed?
有人能告诉我这次怎么减少吗?也许我需要在数据库里做点什么?
不确定我是否正确理解该场景,但您是否尝试在DBContext的OnConfiguring
方法中设置CommandTimeout()
选项。类似这样的东西(EF Core的代码(
protected async override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var dbConn = configuration["DB:Connection"];
optionsBuilder.UseSqlServer(dbConn,
x => x.CommandTimeout(10).EnableRetryOnFailure());
}