【问题标题】:.net core API cant connect PostgreSQL database.net core API 无法连接 PostgreSQL 数据库
【发布时间】:2019-09-17 15:11:25
【问题描述】:

我想使用 .netCore 和 PostgreSQL 数据库创建一个非常简单的 API,这是我的应用设置

 "ConnectionStrings": {
    "DefaultConnection": "Server=192.xxx.xx.xx;Port=5433;Database=ltw_central_db;User Id=UserNAme;Password = myPass; Timeout = 15;"
}

但我收到一条错误消息,指出初始化字符串的格式不符合从索引 0 开始的规范 我用谷歌搜索了这个错误,我了解到这个错误表明我的连接字符串有问题,但似乎我的连接字符串很好,知道吗?

这是我想要 tp 查询的控制器,我得到错误:

   private readonly TenMinutesContext _context;
    public ValuesController(TenMinutesContext context)
    {

        _context = context;
    }
    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        var s = _context.stat10mins.Take(5).ToList();

        return null;
    }

我的 DbContext:

 public TenMinutesContext(DbContextOptions<TenMinutesContext> options) : base(options)
    {
    }
    public DbSet<TenMinutes> stat10mins { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //  modelBuilder.("wpv");
        modelBuilder.Entity<TenMinutes>().ToTable("v_statistics_10_m", "wpv");

        base.OnModelCreating(modelBuilder);
    }

在应用设置中

  public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddDbContext<TenMinutesContext>(opt =>
            opt.UseNpgsql("TenMinutes"));
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        var connection = @"Server=192.xxx.xx.xx;Port=5433;Database=ltw_central_db;User Id=UserNAme;Password = myPass; Timeout = 15;";
        services.AddDbContext<TenMinutesContext>
            (options => options.UseNpgsql(connection));

    }

【问题讨论】:

  • 使用这个连接字符串的代码是什么?
  • 你是否在某个地方定义它应该使用 PostgreSQL,所以它不会尝试使用其他提供者?
  • @SamiKuhmonen 我通过添加 appsetting 更新了我的问题,我使用的是提供程序 Npgsql,我还从 nuget 为 .net 核心安装了 npgsql
  • services.AddDbContext&lt;TenMinutesContext&gt;(opt =&gt; opt.UseNpgsql("TenMinutes")); "TenMinutes" 确实是一个无效的连接字符串。

标签: postgresql asp.net-core


【解决方案1】:
services.AddDbContext<TenMinutesContext>(opt =>
        opt.UseNpgsql("TenMinutes"));

    var connection = @"Server=192.xxx.xx.xx;Port=5433;Database=ltw_central_db;User Id=UserNAme;Password = myPass; Timeout = 15;";
    services.AddDbContext<TenMinutesContext>
        (options => options.UseNpgsql(connection));

你使用了两个AddDbContext,第一个不正确。

我已经使用 localhost 服务器 (Server=localhost) 尝试了您的代码,它会出现与您得到的相同的错误。

当我删除下面的代码时效果很好。

services.AddDbContext<TenMinutesContext>(opt => opt.UseNpgsql("TenMinutes"));

Postgresql中的EF核心,参考:

http://www.npgsql.org/efcore/

【讨论】:

  • 你能告诉我我在哪里使用了两个数据库上下文吗?并通过删除我的代码永远不会到达控制器
  • @mortezasol 查看我的答案的第一行,它是从您发布的代码中复制而来的...
猜你喜欢
  • 2019-07-10
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 2020-08-21
相关资源
最近更新 更多