无法使用Postgres在.NET Core EF迁移中对用户进行身份验证

时间:2018-01-10 22:39:22

标签: postgresql entity-framework asp.net-core

我无法验证EF以在PostgreSQL中执行迁移。我使用Postgre而不是SQL Server来学习本教程:https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db

这是我的appsettings.development.json:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "ConnectionStrings": {
    "DASDBConnection": "Server=localhost;Database=das;User Id=dasdev;Password=password"
  }
}

我可以毫无问题地运行Add-Migration命令。但是,当我尝试迁移时,收到以下错误:

PM> Update-Database
System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'dasdev'.
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   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, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, Func`2 operation, Func`2 verifySucceeded, TState state)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:5e5acc10-e1c3-4b09-8379-2f5c2c8ae866
Error Number:18456,State:1,Class:14
Login failed for user 'dasdev'.

我的依赖项中有这些NuGet包:

Microsoft.AspNetCore.All (2.0.5)
Microsoft.EntityFrameworkCore.Tools (2.0.1)
Microsoft.VisualStudio.Web.Codegeneration.Design(2.0.2)
Npgsql.EntityFrameworkCore.PostgreSQL(2.0.0)

我在数据库服务器上创建了dasdev角色,我可以使用&#39; dasdev&#39;使用psql没有任何问题。登录时提示密码,密码正确。

那么,我错过了什么?

2 个答案:

答案 0 :(得分:1)

我在配置字符串的末尾添加了Integrated Security=true;Pooling=true,验证将通过。似乎Integrated Security=true主要是导致这个问题。

另外,在Startup.cs中,我应该将Npgsql用于DBContext:

services.AddDbContext<DataTypeHere>(option => options.UseNpgsql(connectionStringHere));

希望对使用Postgre尝试.NET Core EF的人有所帮助。

答案 1 :(得分:1)

我相信您也可以使用“用户名”和“密码验证”来进行迁移。我可以看到的问题是您的connectionString对于PostgreSQL应该是这样的:

"DASDBConnection": "User ID=dasdev;Password=password;Host=localhost;Database=das;Pooling=true;"

注意:确保您的用户名和密码有效,以确认直接登录到您的postgres数据库并进行检查。