The underlying provider failed to open , login failed for user IIS APPPOOL

ef-code-first entity-framework sql-server-2008


i am not a sql server guy but i am working on a client's application and want to connect to the database through EF Code first

the Exception message that i have is

The underlying provider failed to open

The Exception details

Cannot open database [DatabaseName] requested by the login. the login failed

login failed for user 'IIS APPPOOL\ASP.NET v4.0"

My web config

<add name="TraininGoDB"
    connectionString="Server=LOCALPCNAME\SQLEXPRESS;Database=[DATABASENAME];Integrated Security=True;"/>

The EF context and entities are defined in a separate project with the app config of the following

<?xml version="1.0" encoding="utf-8"?>
    <!-- For more information on Entity Framework configuration, visit -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
        <parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />

i am using SQL Server Express 2008

don't know the problem from the database configration or from the web config

6/5/2012 12:19:49 PM

Accepted Answer

The user under which the web application pool (associated to your web application) is running doesn't have permissions to access your database. You need to grant it permissions or run under another user who has permissions. So, to summarize: check under which user your application pool is running, then check that that user has access to your database.

6/5/2012 12:32:00 PM

Popular Answer

If all of your Web.config files are correct, SQLSERVER has the appropriate permissions, etc., there's one more thing you can check. I once had this issue when a client had me configure the project so that Entity Framework would not automatically drop the databases (if, for example, the models changed), but I was still testing the project on localhost. I thus needed to create a new database, but I forgot to fix my context files. For example, my problem was:

public MasterContext(string nameOrConnectionString) : base(nameOrConnectionString)

when it should have been (for the purpose of testing):

public MasterContext(string nameOrConnectionString) : base(nameOrConnectionString)
        Database.SetInitializer<MasterContext>(new DropCreateDatabaseIfModelChanges<MasterContext>());


Otherwise, I would receive the same error, that the underlying provider failed on open, with the inner exception being that the login failed for the user.

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