Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider WithQueryTimeout through linq #29804

Open
gokhanabatay opened this issue Dec 8, 2022 · 5 comments
Open

Consider WithQueryTimeout through linq #29804

gokhanabatay opened this issue Dec 8, 2022 · 5 comments

Comments

@gokhanabatay
Copy link

We are trying to migrate ef core 7 from NHibernate. Consider allow us to specify custom timeouts for specific linq statements.

Suggested extension methods could be like below.

"context.Set().WithQueryTimeout(100)"

Is there any way to specify timeout(in seconds) through linq not "context.Database" property.

@roji
Copy link
Member

roji commented Dec 8, 2022

Is there any particular reason you're trying to avoid setting the timeout via context.Database?

@gokhanabatay
Copy link
Author

In NHibernate perspective, we are using future(lazy) queries and those queries not executed in order as you coded.
Also it's good to allow setting timeout via query interface( for ef linq) because developers does not need to set default timeout after specific query executed.

@roji
Copy link
Member

roji commented Dec 9, 2022

In NHibernate perspective, we are using future(lazy) queries and those queries not executed in order as you coded.

This definitely isn't something EF supports; although we would like to one day allow batching multiple queries (#10879), but I'm not sure how they would be ordered otherwise than what the user specifies - maybe you can provide a bit of info on that?

Note that it should be pretty easy to write an extension method over DbSet which accepts a LINQ query and a timeout, and sets the timeout via context.Database before executing the query.

@gokhanabatay
Copy link
Author

For backward compability we need to set timeout though IQuaryable interface is it possible somehow casting to ef internal class?

IQueryable comes through DbSet

image

    public class IQueryableOptions
    {
        protected bool? Cacheable { get; private set; }

        protected int? Timeout { get; private set; }

        protected bool? ReadOnly { get; private set; }

        public IQueryableOptions SetCacheable(bool cacheable)
        {
            Cacheable = cacheable;
            return this;
        }

        public IQueryableOptions SetReadOnly(bool cacheable)
        {
            Cacheable = cacheable;
            return this;
        }

        public IQueryableOptions SetTimeout(int timeout)
        {
            Timeout = timeout;
            return this;
        }

        public static IQueryable<TEntity> Apply<TEntity>(IQueryable<TEntity> query) where TEntity : class
        {
            //TODO EFCORE We can handle IsReadonly but this might cause lazy property access runtime exception
            //https://github.com/dotnet/efcore/issues/10042
            /*if (ReadOnly == true)
            {
                query = query.AsNoTracking();
            }*/

            return query;
        }
    }

This definitely isn't something EF supports; although we would like to one day allow batching multiple queries (#10879), but I'm not sure how they would be ordered otherwise than what the user specifies - maybe you can provide a bit of info on that?

I think feature queries is not major feature considiring other gaps that we are struggling with. Such as ReadOnly/Query Only Navigation properties, Cascading Behaviors.

@roji
Copy link
Member

roji commented Dec 13, 2022

For backward compability we need to set timeout though IQuaryable interface is it possible somehow casting to ef internal class?

You could probably create an extension method directly over DbSet, which extracts the appropriate service and sets the timeout. But you probably wouldn't be able to do it over IQueryable, since at that point the DbSet is already buried inside the expression tree and not very accessible.

@ajcvickers ajcvickers added this to the Backlog milestone Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants