3
votes

I have an Async method in C# that is calling SQL function which is returning true or false and method is returning bool variable that is true or false.

This is my method:

    public async Task<bool> Canlog(string userName, string pass
    {
        Context context = new Context();
        bool queryResult = false;

        using (context )
        {
            using (context .Database.Connection)
            {
                context .Database.Connection.Open();

                string sqlStatment = .....

                queryResult = authorizationContext.Database
                        .SqlQuery<bool>(sqlStatment)
                        .Single();
            }
        }

        return await queryResult;
    }

when I try this, I am getting error in this line return await queryResult that cannot await bool.

1
queryResult is just a bool variable. There is nothing async there. And I also can't see any entity framework there...Florian
@thefiloe framework is not the main concern here so I simplify the code. It is calling the function and return true or false. I need this as a async method.Alma

1 Answers

5
votes

Single() returns <T>, not Task<T>. What you want is SingleAsync():

    return await authorizationContext.Database
                                     .SqlQuery<bool>(sqlStatement)
                                     .SingleAsync();

Also, you can't have your await outside of your using statements because then your DB context will be disposed before the async operation is compelte.