1

I ran a web app on IIS and I got a

System.Data.SqlClient.SqlException: "Login failed for user'domain\account$'".

This is a exception when the function DbMigrator.Update() is called

private readonly DbMigrationsConfiguration<MyContext> _config;
public ContextInitializer(string connectionString)
{
    _config = new Configuration()
    {
        TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient")
    };
}
...

public void InitializeDatabase(MgmtStudioContext context)
{
    var migrator = new DbMigrator(_config);
    migrator.Update();
}

And here is the stack trace:

at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)\r\n   
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)\r\n  
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)\r\n  
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)\r\n   
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)\r\n   
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)\r\n   
at System.Data.SqlClient.SqlConnection.Open()\r\n   
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)\r\n   
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)\r\n   
at System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass33.<UsingConnection>b__32()\r\n   
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()\r\n   
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)\r\n   
at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act)\r\n   
at System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable`1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript)\r\n   
at System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)\r\n   
at System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection)\r\n   
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)\r\n   
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)\r\n   at 

Here is my connection string:

Data Source=(local);Initial Catalog=myTable; Integrated Security=True;

The code works fine in other machine, but in my computer it doesn't work and I can't find out the root cause.

2

2 Answers 2

2

According to the error message, the account 'domain\account$' does not have access to the myTable database.

I suggest you could firstly use the SQL server management system's security to check he account 'domain\account$' does not have access to the (local) server's myTable database.

More details, you could refer to this answer.


Update:

As far as I know, if you want to access SQL Server Using Windows Integrated Security. You should firstly make sure your IIS server and the sql server is inside the same intranet and use the same domain.

Then I suggest you could make sure you have set the right application pool identity account which has the permission to access the sql database.

1.You should the domain account "domain\account" in sql server to make sure it has the read and write permission

2.On IIS, open Application Pool settings.For Identity option, choose "Custom Account". Enter your username domain\account) and password.

Then it will work well.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for replying so late, I didn't found the user name in my sqlserver, so I am wondering how the login user came up. as it is not in sqlserver.
Thanks very much!!! this did solve my problem. I re-bind the ApplicationPool for the website in iis.
1

Please, check the iispool user has permissions to log in to database. You can do this using the Microsoft Sql Server Management Studio. I usually forget it! I Hope its helps you! Greetings.

1 Comment

Hi @Sebas, Do you mean the IIS APPPOOL\AppPoolOfApp in the Sql server? I changed the permission but still not work. Thank you all the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.