5

Error LocalDb when accessing data & ldquo; The underlying provider failed on...

 2 years ago
source link: https://www.codesd.com/item/error-localdb-when-accessing-data-the-underlying-provider-failed-on-open.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Error LocalDb when accessing data & ldquo; The underlying provider failed on Open & rdquo;

advertisements

I'm currently attempting to connect to a local copy of a database for testing purposes as to not alter live data.

I'm using MVC with Entity Framework (.Net 4.5), and I'm currently running into this error whenever I attempt to access data through EF:

An exception of type 'System.Data.EntityException' occurred in System.Data.Entity.dll but was not handled in user code

Additional information: The underlying provider failed on Open.

This is the function that is trying to access the data - the error happens on the first line

public IEnumerable<Client> GetClients()
{
    List<Client> clients = (from c in this.repository.Clients select c).ToList();
    return clients;
}

Here is my connection string in the app.config as well:

<add name="EntitiesConnectionString" connectionString="metadata=res://*/EFName.csdl|res://*/EFName.ssdl|res://*/EFName.msl;provider=System.Data.SqlClient;provider connection string="Data Source=(LocalDb)\v11.0;Initial Catalog=testdb;Integrated Security=True;Pooling=False"" providerName="System.Data.EntityClient" />

I'm using the integrated SQL Server Express that comes with Visual Studio 2015 to manage the local DB that I have created.

More information that I've found:

"Cannot open database \"testdb\" requested by the login. The login failed.\r\nLogin failed for user 'NT AUTHORITY\\SYSTEM'."

I don't believe that I have any permissions set on the db that I created, perhaps something is wrong with the connection string?

Edit:

I've added the 'NT AUTHORITY\NETWORK SERVICE' user, and then added it to the 'db_owner' role in MS SQL Server Management Studio without any luck

I also attempted to create a new login and user with credentials and the 'db_owner' role, and I still receive the same "Login failed for user 'user'."

This is the connection string that I'm using with the test user that I created:

<add name="EntitiesConnectionString" connectionString="metadata=res://*/EFName.csdl|res://*/EFName.ssdl|res://*/EFName.msl;provider=System.Data.SqlClient;provider connection string="Data Source=(LocalDb)\v11.0;Initial Catalog=testdb;Integrated Security=False;Persist Security Info=True;User ID=test;Password=password"" providerName="System.Data.EntityClient" />


You are likely enumerating GetClients() after disposing of the underlying connection.

Try either:

a) changing the return type of the method to something like List< Client>

public List<Client> GetClients()
{
    List<Client> clients = (from c in this.repository.Clients select c).ToList();
    return clients;
}

or b) call .ToList() on the method immediately:

IEnumerable<Clients> clients = GetClients().ToList();

As it is, execution of .ToList() inside your method is deferred until the method's return value is enumerated, which if I had to guess, is after the db connection has been disposed.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK