C# 实体框架,代码优先:避免不必要的azure主数据库连接

C# 实体框架,代码优先:避免不必要的azure主数据库连接,c#,database,entity-framework,azure,azure-sql-database,C#,Database,Entity Framework,Azure,Azure Sql Database,当EF尝试连接到Azure sql数据库时(可能是在尝试打开到数据库的连接时,不确定),它尝试连接/登录到主数据库,问题是为什么 连接字符串上的用户不允许连接到主数据库,从我所看到的记录来看,EF尝试与用户一起登录主数据库失败时出现错误,但从功能上来说,应用程序工作正常(应用程序不需要访问主数据库来做任何事情) 从我的研究来看,它似乎最类似于,但这已经被关闭,因为不可复制,我不确定这是否发生在试图获得ProviderManifestToken时 因此,如果一个数据库用户对其所需的数据库具有正确的

当EF尝试连接到Azure sql数据库时(可能是在尝试打开到数据库的连接时,不确定),它尝试连接/登录到主数据库,问题是为什么

连接字符串上的用户不允许连接到主数据库,从我所看到的记录来看,EF尝试与用户一起登录主数据库失败时出现错误,但从功能上来说,应用程序工作正常(应用程序不需要访问主数据库来做任何事情)

从我的研究来看,它似乎最类似于,但这已经被关闭,因为不可复制,我不确定这是否发生在试图获得ProviderManifestToken时

因此,如果一个数据库用户对其所需的数据库具有正确的权限,并且连接字符串反映了这一点,为什么EF会尝试连接到主数据库

上下文的连接字符串示例

Server=tcp:MyServer;Database=MyDb;User ID=MyDatabaseUser;Password=*****;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;TrustServerCertificate=False;
在下面记录错误消息:

15-05-07 04:06:16.404 System.Data.SqlClient.SqlException (0x80131904): The server principal "MyDatabaseUser" is not able to access the database "master" under the current security context.
Database 'master' on server 'MyServer' is not currently available.  Please retry the connection later.  If the problem persists, contact customer support, and provide them the session tracing ID of '69955F47-7746-41560-A03C-807FB9A56478A'.
Login failed for user 'MyDatabaseUser'.
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
   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)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
   at System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass33.<UsingConnection>b__32()
   at System.Data.Entity.Infrastructure.DbExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func`1 operation)
ClientConnectionId:5a5e2892-0d1f-471c-a1b7-5f9e5705655f1
ErrorCode: 916
ErrorCode: 40613
ErrorCode: 18456
15-05-07 04:06:16.404 System.Data.SqlClient.SqlException(0x80131904):服务器主体“MyDatabaseUser”无法在当前安全上下文下访问数据库“master”。
服务器“MyServer”上的数据库“master”当前不可用。请稍后重试连接。如果问题仍然存在,请联系客户支持,并向他们提供会话跟踪ID“69955F47-7746-41560-A03C-807FB9A56478A”。
用户“MyDatabaseUser”登录失败。
位于System.Data.SqlClient.SqlInternalConnection.OneError(SqlException异常,布尔断开连接,操作'1 wrapCloseInAction)
位于System.Data.SqlClient.TdsParser.ThroweException和Warning(TdsParserStateObject StateObjectStateObj、布尔调用方连接锁、布尔异步关闭)
位于System.Data.SqlClient.TdsParser.TryRun(RunBehavior RunBehavior、SqlCommand cmdHandler、SqlDataReader dataStream、BulkCopySimpleResultSet bulkCopyHandler、TdsParserStateObject stateObj、Boolean和dataReady)
在System.Data.SqlClient.TdsParser.Run(RunBehavior RunBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)
位于System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(布尔值登记OK)
位于System.Data.SqlClient.SqlInternalConnectionDS.AttemptOneLogin(ServerInfo ServerInfo、String newPassword、SecureString newSecurePassword、Boolean IgnoresInOpenTimeout、TimeoutTimer timeout、Boolean withFailover)
位于System.Data.SqlClient.SqlInternalConnectionDS.LoginOfAliver(ServerInfo服务器信息、字符串newPassword、SecureString newSecurePassword、布尔重定向EducserInstance、SqlConnectionString连接选项、SqlCredential凭证、超时超时)
位于System.Data.SqlClient.SqlInternalConnectionDS.OpenLoginList(超时计时器超时、SqlConnectionString连接选项、SqlCredential凭证、字符串新密码、SecureString NewSecureUserPassword、布尔重定向EduceInstance)
位于System.Data.SqlClient.SqlInternalConnectionDS..ctor(DbConnectionPoolIdentity、SqlConnectionString connectionOptions、SqlCredential凭据、对象providerInfo、String newPassword、SecureString newSecurePassword、Boolean redirectedUserInstance、SqlConnectionString userConnectionOptions、SessionData reconnectSessionData)
位于System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions、DbConnectionPoolKey、Object poolGroupProviderInfo、DbConnectionPool、DBConnectionOwningConnection、DbConnectionOptions用户选项)
位于System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool池、DbConnectionOwningObject、DbConnectionOptions选项、DbConnectionPoolKey池键、DbConnectionOptions用户选项)
位于System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection-owningObject、DbConnectionOptions-userOptions、DbConnectionInternal-oldConnection)
位于System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject、DbConnectionOptions userOptions、DbConnectionInternal oldConnection)
位于System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject、UInt32 waitForMultipleObjectsTimeout、Boolean allowCreate、Boolean OnlyOnEckConnection、DbConnectionOptions userOptions、DbConnectionInternal和connection)
位于System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject、TaskCompletionSource`1重试、DbConnectionOptions用户选项、DbConnectionInternal和connection)
位于System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection、TaskCompletionSource`1重试、DbConnectionOptions用户选项、DbConnectionInternal oldConnection、DbConnectionInternal&connection)
位于System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection、DbConnectionFactory connectionFactory、TaskCompletionSource`1重试,DbConnectionOptions用户选项)
位于System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1重试)
位于System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1重试)
在System.Data.SqlClient.SqlConnection.Open()处
在System.Data.Entity.Infrastructure.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget,Action`2 operation,TInterceptionContext interceptionContext,Action`3 executing,Action`3 executed)
位于System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection连接,DbInterceptionContext interceptionContext)
位于System.Data.Entity.SqlServer.SqlProviderServices.c__DisplayClass33.b__u32()
在System.Data.Entity.Infrastructure.DbExecutionStrategy.c_uuDisplayClass1.b_uuu0()中
在System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func`1操作)中
客户连接ID:5a5e2892-0d1f-471c-a1b7-5F9E57055F1
错误代码:916
错误代码:40613
错误代码:18456
实体框架
public static MyContext() : base(null) {}