How to set command timeout in aspnetcore/entityframeworkcore

£可爱£侵袭症+ 提交于 2019-12-06 16:35:54

问题


The place where the command timeout is set is no longer the same as earlier versions.

However, I cannot find anywhere that says how to change this.

What I am doing is uploading very large files which takes longer than the default 30 seconds to save.

Note that I ask about Command Timeout, not Migration Timeout as in another question.


回答1:


If you're using the DI container to manage the DbContext (i.e. you're adding the DbContext to the service collection), the command timeout can be specified in the options.

In Startup.ConfigureServices:

services.AddDbContext<YourDbContext>(options => options.UseSqlServer(
    this.Configuration.GetConnectionString("YourConnectionString"),
    sqlServerOptions => sqlServerOptions.CommandTimeout(60))
);



回答2:


you can change it through your context

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext()
    {
        Database.SetCommandTimeout(150000);
    }
}



回答3:


The better option is to use CommandTimeout during your context setup like:

public class DbConnect: IConnnectDb
{
    private dbentitient _context;

    // inject this to a db entity from constructor. 

    //inside each method now use the follow before u actually run the query to db.  

    _context.Database.SetCommandTimeout(400);
}     

Note: EF Core will only execute the query with less than 100 seconds time. If it's more than that it keeps retrying and you never get to see the result.

That's my experience as of now, so let me know if you are able to fix it EF Core 1.0 does timeout even more fast than EF Core 2.0.



来源:https://stackoverflow.com/questions/39058422/how-to-set-command-timeout-in-aspnetcore-entityframeworkcore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!