Search code examples
entity-frameworkentity-framework-6entity-framework-4entity-framework-5

Can we set dynamically the Command TimeOut by on query execution time in entity framework?


Can we set dynamically the Command TimeOut is "Time of the query execution" in entity framework?


Solution

  • This way:

    using (var context = new EntityContext())
    {
        context.Database.CommandTimeout = 300;
    }
    

    EDIT: Answer comment

    Based on CURD statements, I want set Timeout Limit

    I'm not sure which EF version you are using since you flagged EF6, EF5 and EF4.

    I will assume in my answer EF6

    You can use DbCommandInterceptor: https://www.entityframeworktutorial.net/entityframework6/database-command-interception.aspx

    To check the CommandText and set the CommandTimeout based on it.