开发者

Does Linq to Entities throw SqlExceptions?

I'm usin开发者_运维问答g linq to entities, and my entity model is sitting on top of MSSQL database.

I'm wondering if linq to entities throws SqlExceptions or not.

In other words, will the below code catch exception successfully if there was a problem connecting to the database?

If it doesn't, what's the best way to handle exceptions when using linq to entities?

        using (MyUserEntities userEntities = new MyUserEntities(connectionString))
        {
            try 
            {
                if (userEntities.Users.Any<User>(userInDB =>
                                                 userInDB.UserName == username))
                    {
                       //Do Something
                    }
                    else
                    {
                       //Do Something else
                    }
            }
            catch(SqlException e)
            {
            }
        }


Yes it will, If any of the processes in your function causes a problem,

userEntities.Users.Any<User>(userInDB =>
                                             userInDB.UserName == username)

It will throw a cascading exception whitch will be caught.

Altough there is a specific timeout setting for db connections, so sometimes this takes a while.

Why didn't you test it ? you could have found out this answer yourself

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜