Azure WebJobs无法连接到mdf数据库

时间:2017-08-31 20:46:03

标签: asp.net-mvc azure-webjobs mdf

我正在开发Azure网站和WebJob。开发数据库是一个Mdf文件。 Mvc应用程序可以连接到Azure存储和数据库。 WebJob可以连接到Azure存储,但数据库会抛出:

System.Data.SqlClient.SqlException occurred
  HResult=0x80131904
  Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid.
)
  Source=Core .Net SqlClient Data Provider
  StackTrace:
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)

Mvc和WebJob共享相同的连接字符串:

Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=D:\\Projects\\Test1\\Code\\Database\\Test1.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False

如果在启动WebJob时Mvc应用程序正在运行,则没有任何区别。

1 个答案:

答案 0 :(得分:0)

在MVC app用户机密中,我不得不使用双斜杠,如下所示:

Data Source=(LocalDB)\\MSSQLLocalDB

在webjob控制台应用的app.config文件中,我不得不使用这样的单斜杠:

Data Source=(LocalDB)\MSSQLLocalDB