Linq queries do not raise an exception for database connection errors

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
Ameen
Posts: 1
Joined: Fri 10 Mar 2017 17:14

Linq queries do not raise an exception for database connection errors

Post by Ameen » Fri 10 Mar 2017 18:37

Hello,

I am currently experimenting with LinqConnect. The DB class opens a connection to a MySQL database that will fail. I was expecting the Linq query to raise an exception but it didn't. The variable contacts has an error message "Error on opening DbConnection.", though. Is there a way to get DataContext an exception with the database connection fails ?

Thanks

Code: Select all

                try
                {
                    var db = new DB();
                    var contacts = (from c in db.People select c);
                    return contacts;
                } catch (Exception e)
                {
                    Console.WriteLine(e);
                    return Negotiate
                        .WithModel(new Failure() { error = "database error" })
                        .WithStatusCode(HttpStatusCode.InternalServerError);
                }

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Linq queries do not raise an exception for database connection errors

Post by Shalex » Tue 14 Mar 2017 12:05

Please use

Code: Select all

catch (Exception ex) {
    Console.WriteLine("Exception: " + ex.Message);
    while (ex.InnerException != null) {
        ex = ex.InnerException;
        Console.WriteLine("Inner Exception: " + ex.Message);
    }
}
instead of

Code: Select all

catch (Exception e) {
    Console.WriteLine(e);
}

Post Reply