c# - Npgsql.NpgsqlException: '没有提供密码
问题描述
我是 dotnet core、Entity framework core 和 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);
}
下面是我的控制器类如下:
[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();
}
}
但我在控制器类的构造函数中收到错误,因为 Npgsql.NpgsqlException:'没有提供密码,但后端需要一个(在 MD5 中)'
我已经搜索了 2 个小时,但在我的情况下没有任何效果。有人建议我只需要在一个我已经在做的地方使用连接。
无论如何,请建议我摆脱这个错误。
解决方案
您错过了连接字符串中登录名和密码之间的分号。请看例子
推荐阅读
- php - Woocommerce wc_get_price_including_tax 仍然返回前。增值税价格(后端)
- java - 在 3d 数组 Java 中查找值的位置
- postgresql - 为什么 to_jsonb() 在 postgresql 中将 bigint 转换为数字?
- java - Java反射获取字段值
- c# - 如何在 ASP.NET Core 中自动配置服务?
- h2 - 在 H2 中选择但带有数据库前缀
- python - 如何在 discord.py 中使用 Tenor api 获得热门搜索结果并返回随机 gif
- python - 匹配文本文件中的字符串块
- microsoft-graph-api - masterCategories 端点不支持 URL 参数
- html - 在表格中居中两行文本