如何在Visual Studio中针对SQL Azure从包管理器控制台调用更新数据库

本文关键字:管理器 控制台 调用 数据库 更新 Azure SQL Visual Studio | 更新日期: 2023-09-27 18:19:27

我使用的是EF 6.1.3。我正在尝试从包管理器控制台调用针对SQL Azure的更新数据库。使用本地SQL Express 2012一切正常。我可以使用SQL server Management Studio 2012/2014和具有相同凭据的Visual Studio服务器资源管理器成功连接到服务器。我已经从管理门户对SQL Azure防火墙进行了例外处理,但我收到了错误:

错误编号:18456,状态:1,类别:14用户"xxxxxxx"登录失败。已为该会话分配了一个跟踪ID"xxxxxxx-xxxx-xxxx-xxxxxxxxxxxxx"。当您需要帮助时,请将此跟踪ID提供给客户支持。

例外情况是:

System.Data.SqlClient.SqlException(0x80131904):用户"xxxxx"登录失败。已为该会话分配跟踪ID"xxxxxx"。当您需要帮助时,请将此跟踪ID提供给客户支持。位于System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,Boolean allowCreate,Boolean onlyOneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal&connection)位于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)在System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1重试,DbConnectionOptions userOptions)位于System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1重试)位于System.Data.SqlClient.SqlConnection.Open()位于System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b_36(DbConnection t,DbConnectionInterceptionContext c)在System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action 2操作,执行TInterceptionContext interceptionContext,操作3 executing, Action 3)位于System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection连接,DbInterceptionContext interceptionContext)位于System.Data.Entity.SqlServer.SqlProviderServices。<>c_DisplayClass33.b_32()位于System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy。<>c_DisplayClass1.b_0()位于System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](函数1 operation) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) at System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action 1动作)位于System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection,Action 1 act) at System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable 1 commandTimeout,DbConnection sqlConnection,String createDatabaseScript)位于System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection连接,可为Null的1 commandTimeout, StoreItemCollection storeItemCollection) at System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable 1命令超时,StoreItemCollection StoreItemCollection)位于System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()位于System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection连接)位于System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(操作必须成功ToKeepDatabase)位于System.Data.Entity.Migrations.Infrastructure.MigorBase.EnsureDatabaseExists(操作必须成功到KeepDatabase)位于System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)位于System.Data.Entity.Migrations.Infrastructure.MigorBase.Update(String targetMigration)位于System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()位于System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)位于System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)位于System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)位于System.Data.Entity.Migrations.Design.ToolingFacade.Update(字符串targetMigration,布尔力)位于System.Data.Entity.Migrations.UpdateDatabaseCommand。<>c_DisplayClass2.<。ctor>b_0()位于System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(操作命令)客户端连接ID:xxxxx

我使用的连接字符串是:

更新数据库-SourceMigration xxxxxx-TargetMigration xxxxx-StartUpProjectName xxxxx-ProjectName xxxxx.Migrations-ConfigurationTypeName"xxxxx.MigurationConfiguration"-ConnectionString"Server:tcp:xxxxxx.Database.windows.net,1433;Database=xxxxxx;用户ID=xxxxxx;密码=xxxxxx;Trusted_Connection=False;加密=True;连接超时=30;"-Connection ProviderName"System.Data.SqlClient"-强制

我从门户获得了连接字符串。有人知道问题在哪里,或者如何解决这个问题吗?

我在错误消息中只发现一件奇怪的事情:用户"xxxxxxx"登录失败。用户实际上xxxx@serverName.这里有什么逃跑的把戏吗?

如何在Visual Studio中针对SQL Azure从包管理器控制台调用更新数据库

正如我所怀疑的那样-用户名中的@字符破坏了它。由于我不知道的原因,为Sql Azure生成的用户名是username@serverName.当您从Azure管理门户获得连接字符串参数并将其放在双引号中用于EF迁移时:

-ConnectionString"connStringusername@serverName"

你得到了"username@serverName"截断,因此基础连接提供程序只使用"username"而不使用"username@serverName".

实体框架将用户名读取为"username",而不是"username@serverName"然后你会被服务器拒绝。几乎每个程序都有智能来检查你是否放置了@serverName后缀,并在幕后耍花招。EF有点严格,但没有。(我喜欢它)

这个问题的解决方法很简单——把引号转过来。用单引号将连接字符串参数括起来,用双引号将用户名括起来,如下所示:

-ConnectionString'connString with"username@serverName"….

通过这种方式,用户名作为一个整体被保存,而不是在@字符处被截断,EF正确连接。