How to avoid connections against empty/null database by Entity Framework

.net c# entity-framework entity-framework-6 sql-server


We use entity framework to read from an existing database. This is a simplified version of our code.

using (my context context = new mycontext())
    if(context.Database.Connection.State == System.Data.ConnectionState.Closed)
        _logger.Info(" Opening the connection to the database");

    context.Configuration.LazyLoadingEnabled = false;          
    IQueryable<mymodel> people;
    people = context.People.OrderBy(x => x.Firstname);
    _lstContacts = people.ToList();

    if (context.Database.Connection.State != System.Data.ConnectionState.Closed)
        _logger.Info(" Connection to the database Closed");

It works 100% of the time, but... On our UAT environment we can see failed connections to the Microsoft SQL server with the error:

Login failed for user "my user". Reason: Failed to open the explicitly specified database "null". Client my IP.

For us, these are ghost connections because at the time when we see the errors in the SQL server, our code is not executed. Initially we didn't close and open the connection explicitly, we just added it trying to control when EF open and closes the connection, but it didn't fix the issue.

Our connection string is using the following format:

<add name="MYCN" connectionString="metadata=res://*/CVs.Cvs.csdl|res://*/CVs.Cvs.ssdl|res://*/CVs.Cvs.msl;provider=System.Data.SqlClient;provider connection string="data source=myserver\;initial catalog=mydatabase;Integrated Security=;User ID=myuser;Password=XXXXXXX;MultipleActiveResultSets=True;App=EntityFramework"/>

As you can see, we are specifying the database in the connection string and our user only have access to our database, so we understand the error when EF doesn't include the database in the connection string, but we don't understand why it's trying to perform these connections. We know the connections are coming from our application, because we are the only one using that specific user, the IP is the IP of our server, and because the logs in SQL server tell us that the application is "EntityFramewrok"

8/16/2019 5:48:05 PM

Popular Answer

I didn't personally see the error before, but researched for you and seen that many people suffered from the same problem discussed here:

I read all the messages in the website specified, and here are the solutions offered and at least one other user confirmed that it worked. You might not use 2005 as you didn't specify your version in your question, some solutions I believe will still work for you. Try the list below.

Solution list:

1) Please check the state number of this error and search solution by the state number in addition to the message, might give your more accurate solution proposals. Most common states are listed: List item

All state-error descriptions you can find here:

2) Make sure the username and password are correct.


  • Logon to SQL Server using windows authentication.
  • Right click in the query window that appears and select "Open Server in Object Explorer"
  • Go to object explorer and open the "Security" folder and then the "Logins" folder.
  • Double-click the "sa" user and change the password. Also, like another user mentioned above, untick the "Enforce Password Policy" in addition to resetting the password.

4) Try to change the password and turn off the policy, and try with new password.

exec sp_password @new = ‘sqlpassword’, @loginame = ‘sa’
alter login sa
with password = ‘sqlpassword’ unlock,
check_policy = off,
check_expiration = off

5) Run your application/browser and SSMS (if you work on it) in administration mode.


  • Open Windows Services
  • Configure Microsoft Single Sign-on Service to use the proper account
  • Open Central Administration >> Operations >> Manage settings for single sign-on
  • Configure properties to use the same account used for Microsoft ‘Single Sign-on Service

7) Go to Sql server configuration manager and Enable TCP/IP and named pipes


  • go to sql server
  • right click on server, choose properties
  • click on security
  • on server authentication, enable SQL Server authentication

These might help:

8/13/2019 8:43:46 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow