如何使用移动服务避免与Azure主数据库的不需要的连接?

时间:2015-04-24 17:04:06

标签: entity-framework azure azure-sql-database azure-mobile-services

以下代码:

    private void IsolateIssue()
    {
        using (var context = new SomeName.Models.MobileServiceContext())
        {
            System.Diagnostics.Debug.WriteLine(context.Database.Connection.ConnectionString);
            var numberOfItems = context.TodoItems.Count();
        }
    }

导致错误(调用var numberOfItems时):

  

发生了'System.Data.SqlClient.SqlException'类型的异常   EntityFramework.dll但未在用户代码中处理   信息:无法打开登录请求的数据库“master”。该   登录失败。用户'SomeLoginName'登录失败。

这是我的connectionString:

  

“数据源= ServerName.database.windows.net;初始   目录= SomeDataBaseName;用户   ID = SomeLoginName;密码= SomePassword};异步   加工= TRUE; TrustServerCertificate = FALSE;“

很明显它无法连接到master数据库。鉴于用户名和密码,我也无法使用LinqPad连接到master数据库(当我尝试时遇到类似的错误)。但是,它根本不应该连接到master数据库。当我尝试使用Linqpad登录SomeDataBaseName时,我成功获取了todoItems的数量(17)。这证明根本不需要访问master数据库。

首先,我必须确保能够获得移动服务中的项目数量。我怎么做?初始目录显示它应该连接到SomeDataBaseName,并且不需要连接到master数据库来获取项目数。我如何避免它试图这样做?

如果有帮助,这里是stacktrace:

  

StackTrace“at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception,Boolean breakConnection,Action 1 wrapCloseInAction)\r\n at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)\r\n at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)\r\n at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)\r\n at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)\r\n at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)\r\n at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)\r\n at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)\r\n at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)\r\n at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)\r\n at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)\r\n 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&amp; connection)\ r \ n在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在System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource 1 retry, DbConnectionOptions userOptions)\r\n at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1重试)\ r \ n \ n在System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1 retry)\r\n at System.Data.SqlClient.SqlConnection.Open()\r\n at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c)\r\n at System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch [TTarget,TInterceptionContext](TTarget target,Action 2 operation, TInterceptionContext interceptionContext, Action 3执行,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__DisplayClass34.<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操作)\ r \ n at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)\ r \ n在System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection,Action 1 act)\r\n at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action 1 act)\ r \ n \ n在System.Data.Entity.SqlServer.SqlProvi在SER.Data.Entity.Core.Common.DbProviderServices.DeleteDatabase(DbConnection连接,Nullable {{1} (TContext context)\ r \ n在System.Data.Entity.Internal.InternalContext。&lt;&gt; c__DisplayClassf 1 commandTimeout, String databaseName)\r\n at System.Data.Entity.SqlServer.SqlProviderServices.DbDeleteDatabase(DbConnection connection, Nullable 1.PerformAction(TInput input)\ r \ n在System.Data.Entity.Internal.LazyInternalContext .InitializeDatabaseAction(Action 1 commandTimeout, StoreItemCollection storeItemCollection)\r\n at System.Data.Entity.Core.Objects.ObjectContext.DeleteDatabase()\r\n at System.Data.Entity.Internal.DatabaseOperations.Delete(ObjectContext objectContext)\r\n at System.Data.Entity.Database.Delete()\r\n at System.Data.Entity.DropCreateDatabaseIfModelChanges 1.Initialize()\ r \ n在System.Data.Entity.Internal.Linq.InternalSet 1.<CreateInitializationAction>b__e()\r\n at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)\r\n at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()\r\n at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)\r\n at System.Data.Entity.Internal.RetryAction 1.System.Linq.IQueryable.get_Provider()\ r \ n在System.Linq.Queryable.Count [TSource](IQueryable`1 source)\ r \ n

1 个答案:

答案 0 :(得分:2)

我在你的堆栈中看到了这个:System.Data.Entity.DropCreateDatabaseIfModelChanges。我相信EF需要访问master数据库才能删除并重新创建数据库。

此博客文章提到了ClearDatabaseSchemaIfModelChanges初始化程序,它可能会执行您想要的操作:http://blogs.msdn.com/b/azuremobile/archive/2014/06/07/fresh-updates-to-azure-mobile-services-net.aspx