【发布时间】:2015-04-24 17:04:05
【问题描述】:
以下代码:
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”登录失败。
这是我的连接字符串:
"数据源=ServerName.database.windows.net;初始 目录=SomeDataBaseName;用户 ID=SomeLoginName;Password=SomePassword};异步 处理=True;TrustServerCertificate=False;"
很明显是连接不上master数据库。给定用户名和密码,我也无法使用 LinqPad 连接到主数据库(当我尝试时,我得到了类似的错误)。但是,它根本不应该连接到主数据库。当我尝试使用 Linqpad 登录 SomeDataBaseName 时,我成功获得了 todoItems 的数量(17)。这证明根本不需要为了给定的目的访问主数据库。
首先,我必须确保我可以获取移动服务中的商品数量。我怎么做?初始 Catalog 显示它应该连接到 SomeDataBaseName 并且不需要连接到主数据库来获取项目数。我如何避免它试图这样做?
如果有帮助,这里是堆栈跟踪:
StackTrace " 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔 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, TaskCompletionSource1 重试,DbConnectionOptions userOptions,DbConnectionInternal& 连接)\r\n 在 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection( DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)\r\n at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)\r\n 在 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)\r\n at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry)\r\n 在 System.Data。 SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 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.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget 目标,Action2 operation, TInterceptionContext interceptionContext, Action3 正在执行,Action3 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](Func1 操作)\r\n 在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy .Execute(Action 操作)\r\n at System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action1 act)\r\n at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action1 act)\r\n at System.Data.Entity.SqlServer.SqlProviderServices.DropDatabase(SqlConnection sqlConnection, Nullable1 commandTimeout, String databaseName)\r\n at System.Data.Entity.SqlServer.SqlProviderServices.DbDeleteDatabase(DbConnection connection, Nullable1 commandTimeout, StoreItemCollection storeItemCollection)\r\n 在 System.Data.Entity.Core.Common.DbProviderServices.DeleteDatabase(DbConnection 连接, Nullable@987654330 @1.InitializeDatabase(TContext 上下文)\r\n 在 System.Data.Entity.Internal.InternalContext.c__DisplayClassf1.<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.RetryAction1.PerformAction(TInput 输入)\r\n 在 System.Data.Entity.Internal.LazyInternalContext .InitializeDatabaseAction(Action1 action)\r\n at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()\r\n at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\r\n at System.Data.Entity.Internal.Linq.InternalSet1.Initialize()\r\n 在 System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext()\r\n at System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider()\r\n 在 System.Linq .Queryable.Count[TSource](IQueryable`1 源)\r\n
【问题讨论】:
-
你在使用 TransactionScope 吗?
-
当我在我的项目中搜索“TransactionScope”时,我没有得到任何结果。所以这可能意味着:“不”。
-
不使用TransactionScope是不是很傻?如果我要创建一个新数据库并在 TransactionScope 内执行所有事务,是否会不再出现此类问题?
-
不,我刚刚在堆栈上看到了“Enlist”这个词。这通常表示分布式事务。 TS很容易意外导致它们。我不知道是什么原因导致您面临的问题。我会查看 EF 源代码,跟踪异常中的堆栈并找出 EF 显式引用 [master] 的原因。记录一个错误,团队反应迅速。
标签: entity-framework azure azure-sql-database azure-mobile-services