0

I want to create a very simple API using .netCore and PostgreSQL database, here is my app setting

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

but I get an error which says Format of the initialization string does not conform to specification starting at index 0 I googled the error and i learned this error indicates something is wrong with my connection string but seems my connection string is fine, any idea?

here is the controller I want tp query and i get error:

   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;
    }

My 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);
    }

In Appsetting

  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));

    }
4
  • What is the code using this connection string? Commented Sep 15, 2019 at 8:42
  • Do you somewhere define that it should use PostgreSQL so it’s not trying to use some other provider? Commented Sep 15, 2019 at 8:49
  • @SamiKuhmonen i updated my question by adding appsetting,there im using Npgsql which is the provider,and i also installed npgsql for .net core from nuget Commented Sep 15, 2019 at 8:53
  • services.AddDbContext<TenMinutesContext>(opt => opt.UseNpgsql("TenMinutes")); "TenMinutes" is indeed an invalid connection string. Commented Sep 15, 2019 at 11:23

1 Answer 1

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));

You have used two AddDbContext and the first one is incorrect.

I have tried your code using localhost server (Server=localhost) and it will have the same error like what you have gotten.

It works well when I remove below code.

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

For EF core in Postgresql, refer to:

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

Sign up to request clarification or add additional context in comments.

2 Comments

can you show me where i have used two db context?and by removing that my code never reaches controller
@mortezasol see the first line of my answer which copied from your posted code...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.