c# - Npgsql.Npgsql异常 : 'No password has been provided

标签 c# postgresql .net-core entity-framework-core

我是 dotnet 核心、 Entity Framework 核心和 PostgreSQL 的初学者。我正在 ConfigureServices 方法中与数据库建立连接,如下所示:

public void ConfigureServices(IServiceCollection services)
    {            
        services.AddOptions();
        string databaseConnection = "Server=localhost;Port=5432;Database=EF_Lesson Username=postgres password=123;Integrated Security=false;";
        services.AddDbContext<EF_LessonContext>(
            options => options.UseNpgsql(databaseConnection));
 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

下面是我的Controller类如下:

[Route("ToLesson")]
[ApiController]
public class EF_LessonController : ControllerBase
{
    private readonly EF_LessonContext _context;
    public EF_LessonController(EF_LessonContext context)
    {
        _context = context;
        if (_context.Webserverlogin.Count() == 0)
        {
            _context.Webserverlogin.Add(new Webserverlogin() {  });
            _context.SaveChanges();
        }
    }

    [HttpGet]
    public ActionResult<List<Webserverlogin>> GetAll()
    {
        return _context.Webserverlogin.ToList();
    }
}

但我在 Controller 类的构造函数中收到一个错误,如 Npgsql.NpgsqlException: 'No password has been provided but the backend requires one (in MD5)'

我已经搜索了 2 个小时,但在我的案例中没有任何效果。有人建议我只需要在一个地方使用连接,而我已经在这样做了。

无论如何请建议我摆脱这个错误。

最佳答案

您在连接字符串中错过了登录名和密码之间的分号。请参阅example

关于c# - Npgsql.Npgsql异常 : 'No password has been provided,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53754946/

相关文章:

c# - 用于 Web API 单元测试的模拟请求对象

c# - 为什么我们不能像使用 WCF 或 ASMX 一样在 Visual Studio 中将 Web API 添加为 "service reference"?

postgresql - 定义 AWS RDS Postgres session 限制

c# - 如何使用 "dotnet test"按类别过滤 NUnit 测试

c# - 找不到类型或命名空间 AppServiceConnection

C# 可插拔架构,论文考虑

PostgreSQL 同表左外连接多表

PostgreSQL OUTER 连接逻辑

.net - 在 sles 11 中运行 .NET

selenium - .NET Core Selenium WebDriver 未找到