如何建立与远程数据库 sql 服务器的连接?

How to establish connection to remote database sql server?

当尝试使用 enable-migrations 连接到远程数据库时,我收到一个错误,坚持检查连接字符串,我想做的就是将我的上下文连接到远程数据库提供并映射一个“用户”class 与“迁移”, 这是连接字符串:

<add name="TransformerANDConnectDB" connectionString="data source=REMOTE_SERVER_xx ;initial catalog=TransformerANDConnectDB;user id=xxx ;password=xxx ;Integrated Security=True ;MultipleActiveResultSets=True;"  providerName="System.Data.SqlClient" />

不然我给你介绍启用迁移后出现的错误:

 ---> System.Data.SqlClient.SqlException: Échec de l'ouverture de session de l'utilisateur 'ADSLOCAL\G558253'.
  à  System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString 

An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.

这是我的上下文文件:

public class TransformerConnectMEContext  :DbContext 
{
    public TransformerConnectMEContext()
            : base("Name=TransformerANDConnectDB")
    {
        Database.SetInitializer<TransformerConnectMEContext>(new TransformerConnectMEContextInitializer());
    }
    public DbSet<User> Users { get; set; }
}

内部异常:

System.Data.Entity.Core.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.Data.SqlClient.SqlException: Échec de l'ouverture de session de l'utilisateur 'ADSLOCAL\XXXX'.
   à System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
   à System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   à System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   à 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 onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   à System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   à 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)
   à System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   à System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry)
   à System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1 retry)
   à System.Data.SqlClient.SqlConnection.Open()
   à System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__38(DbConnection t, DbConnectionInterceptionContext c)
   à System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action 2 operation, TInterceptionContext interceptionContext, Action 3 executing, Action 3 executed)
   à System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
   à System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass31.<UsingConnection>b__2f()
   à System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   à System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func 1 operation)
   à System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
   à System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action 1 act)
   à System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action 1 act)
   à System.Data.Entity.SqlServer.SqlProviderServices.GetDbProviderManifestToken(DbConnection connection)
   à System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
   --- Fin de la trace de la pile d'exception interne ---
   à System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
   à System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
   --- Fin de la trace de la pile d'exception interne ---
   à System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
   à System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.<>c__DisplayClass1.<ResolveManifestToken>b__0(Tuple 3 k)
   à System.Collections.Concurrent.ConcurrentDictionary 2.GetOrAdd(TKey key, Func 2 valueFactory)
   à System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection)`
  

在连接字符串中将 Integrated Security 设置为 false。

当 Integrated Security 设置为 false 时,将在连接中指定用户 ID 和密码。为真时,当前 Windows 帐户凭据用于身份验证