如何从 Visual Studio 中的包管理器控制台针对 SQL Azure 调用更新数据库?
How to call update-database from package manager console in Visual Studio against SQL Azure?
我正在使用 EF 6.1.3。我正在尝试从包管理器控制台针对 SQL Azure 调用更新数据库。本地 SQL Express 2012 一切正常。我可以使用 SQL Server Management Studio 2012/2014 和具有相同凭据的 Visual Studio Server Explorer 成功连接到服务器。我已经从管理门户对 SQL Azure 防火墙进行了例外处理,但我收到错误:
Error Number:18456,State:1,Class:14
Login failed for user 'xxxxxxx'.
This session has been assigned a tracing ID of 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'. Provide this tracing ID to customer support when you need assistance.
例外情况是:
System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'xxxxx'.
This session has been assigned a tracing ID of 'xxxxxx'. Provide this tracing ID to customer support when you need assistance.
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, TaskCompletionSource1 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, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__36(DbConnection t, DbConnectionInterceptionContext c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action
2 operation, TInterceptionContext interceptionContext, Action3 executing, Action
3 executed)
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass33.b__32()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
at System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action
1 act)
at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action1 act)
at System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable
1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript)
at System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable1 commandTimeout, StoreItemCollection storeItemCollection)
at System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable
1 commandTimeout, StoreItemCollection storeItemCollection)
at System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()
at System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection)
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
ClientConnectionId:xxxxx
我使用的连接字符串是:
Update-Database -SourceMigration xxxxxx -TargetMigration xxxxx -StartUpProjectName xxxxx -ProjectName xxxxx.Migrations -ConfigurationTypeName "xxxxx.MigrationConfiguration" -ConnectionString "Server=tcp:xxxxxx.database.windows.net,1433;Database=xxxxxx;User ID=xxxxxx;Password=xxxxxx;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" -ConnectionProviderName "System.Data.SqlClient" -Force
我从门户网站获得了连接字符串。有谁知道陷阱在哪里或如何解决这个问题?
我在错误消息中只发现一件奇怪的事情:用户 'xxxxxxx' 登录失败。该用户实际上是 xxxx@serverName。是不是有什么越狱技巧?
如我所料 - 用户名中的 @ 字符破坏了它。出于我不知道的原因,为 Sql Azure 生成的用户名是 username@serverName。当您从 Azure 管理门户获取连接字符串参数并将其用于 EF 迁移时用双引号引起来:
-ConnectionString "connString with username@serverName"
您得到了 "username@serverName" 截止点,因此底层连接提供程序仅使用 "username" 而不是 "username@serverName"。
Entity Framework 将用户名读取为 "username" 而不是 "username@serverName" 并且您会被服务器拒绝。几乎每个程序都有智能来检查您是否放置了@serverName 后缀,并在幕后进行操作。 EF有点严格,但没有。 (而且我喜欢)
解决问题的方法很简单——把引号转过来。用单引号括起连接字符串参数,用双引号括起用户名:
-ConnectionString 'connString with "username@serverName" .... '
这样,用户名作为一个整体保留下来,不会在 @ 字符处被截断,并且 EF 可以正确连接。
我正在使用 EF 6.1.3。我正在尝试从包管理器控制台针对 SQL Azure 调用更新数据库。本地 SQL Express 2012 一切正常。我可以使用 SQL Server Management Studio 2012/2014 和具有相同凭据的 Visual Studio Server Explorer 成功连接到服务器。我已经从管理门户对 SQL Azure 防火墙进行了例外处理,但我收到错误:
Error Number:18456,State:1,Class:14 Login failed for user 'xxxxxxx'. This session has been assigned a tracing ID of 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'. Provide this tracing ID to customer support when you need assistance.
例外情况是:
System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'xxxxx'. This session has been assigned a tracing ID of 'xxxxxx'. Provide this tracing ID to customer support when you need assistance. 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, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry) at System.Data.SqlClient.SqlConnection.Open() at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__36(DbConnection t, DbConnectionInterceptionContext c) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action
2 operation, TInterceptionContext interceptionContext, Action3 executing, Action
3 executed) at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) at System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass33.b__32() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.b__0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) at System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action
1 act) at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action1 act) at System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable
1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript) at System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable1 commandTimeout, StoreItemCollection storeItemCollection) at System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable
1 commandTimeout, StoreItemCollection storeItemCollection) at System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase() at System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection) at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run() at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force) at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) ClientConnectionId:xxxxx
我使用的连接字符串是:
Update-Database -SourceMigration xxxxxx -TargetMigration xxxxx -StartUpProjectName xxxxx -ProjectName xxxxx.Migrations -ConfigurationTypeName "xxxxx.MigrationConfiguration" -ConnectionString "Server=tcp:xxxxxx.database.windows.net,1433;Database=xxxxxx;User ID=xxxxxx;Password=xxxxxx;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" -ConnectionProviderName "System.Data.SqlClient" -Force
我从门户网站获得了连接字符串。有谁知道陷阱在哪里或如何解决这个问题?
我在错误消息中只发现一件奇怪的事情:用户 'xxxxxxx' 登录失败。该用户实际上是 xxxx@serverName。是不是有什么越狱技巧?
如我所料 - 用户名中的 @ 字符破坏了它。出于我不知道的原因,为 Sql Azure 生成的用户名是 username@serverName。当您从 Azure 管理门户获取连接字符串参数并将其用于 EF 迁移时用双引号引起来:
-ConnectionString "connString with username@serverName"
您得到了 "username@serverName" 截止点,因此底层连接提供程序仅使用 "username" 而不是 "username@serverName"。
Entity Framework 将用户名读取为 "username" 而不是 "username@serverName" 并且您会被服务器拒绝。几乎每个程序都有智能来检查您是否放置了@serverName 后缀,并在幕后进行操作。 EF有点严格,但没有。 (而且我喜欢)
解决问题的方法很简单——把引号转过来。用单引号括起连接字符串参数,用双引号括起用户名:
-ConnectionString 'connString with "username@serverName" .... '
这样,用户名作为一个整体保留下来,不会在 @ 字符处被截断,并且 EF 可以正确连接。