Can't open sqlconnection within nunit test

后端 未结 1 1784
时光说笑
时光说笑 2021-01-27 11:21

I\'m having a weird issue that I can\'t figure out. I\'m trying to write some integration tests around some database code and my unit tests fail with a weird exception. Running

相关标签:
1条回答
  • 2021-01-27 12:11

    Recently, I experienced an exception with the same call stack.

    Enviroment

    I was using LocalDB server on Windows 8.1 64-bit (not in domain) with default UAC settings and the user is local admin, Framework v4.0.2 (see why it's important). Sadly, the issue can't be reproduced on other computers with the same enviroment.

    Issue description

    Found out that the exception occurs in the following conditions:

    • There is Integrated Security parameter in the connection string which is used to create a new database.
    • The same process opens a connection to the newly created DB after it got created.

    Breaking one of these two conditions allows to get through without exceptions. So omitting the Integrated Security is the easiest way to go. As an option - don't open connection to the newly created DB in the same process. But just adding a pause after creating a DB wouldn’t help if we’re still working in the scope of one process.

    Solution

    Chuck out the Integrated Security parameter in the connection string when have a deal with LocalDB.

    General Comments

    Integrated Security is an option, which specifies that Windows account credentials are used for authentication (see details on MSDN and here). Having this option (value True or SSPI) is recommended for SQL Servers.

    Meanwhile this option doesn’t make any difference for LocalDB servers. Regardless of this setting, LocalDB will still be a user instance, running under the account that started it. Basically, it’ll be as Integrated Security was set to True or SSPI. There is no way in LocalDB to prevent the user from accessing data in their database (e.g. by using SQL authorisation). Once the user has access to the database file, they can spin up their own LocalDB instance and open the database.

    Hence all recommendations to specify the Integrated Security when connecting to LocalDB have no grounds.

    0 讨论(0)
提交回复
热议问题