EF Core with Postgres connection string error

asp.net-core asp.net-core-mvc entity-framework-core

Question

I am trying to use ASP.net Core along with postgres for the very first time. I was following (http://dotnetthoughts.net/using-postgresql-with-aspnet-core/) this blog post to create my first migration. I already created a database in my posgresSQL called asp_api_test.

My dotnet ef migrations add Initial was successful. However when I ran dotnet ef database update I ran into error Format of the initialization string does not conform to specification starting at index 0

My appsettings.json:

{
  "ConnectionStrings": {
    "DataAccessPostgreSqlProvider": "Host=localhost;Username=postgres;Password=root;Database=asp_trial_api;Pooling=true;"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

I am guessing my connection string is incorrect. My startup.cs:

public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddDbContext<WebAPIDataContext>(options => {
                options.UseNpgsql("DataAccessPostgreSqlProvider", b => b.MigrationsAssembly("New_Api"));
            });

            services.AddMvc();
        }

My project.json:

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.EntityFrameworkCore": "1.0.0",
    "Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    },
    "Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.0",
    "Microsoft.ApplicationInsights.AspNetCore": "2.0.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

What am I doing wrong?

1
1
2/24/2017 9:49:06 PM

Popular Answer

I updated to following-

appsettings.js:

{
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "Host=localhost;Username={{postgres}};Password={{root}};Database={{asp_trial_api}}"
    }
},
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

and in my startup.cs:

services.AddDbContext<WebAPIDataContext>(options => {
                options.UseNpgsql(Configuration["Data:DefaultConnection:ConnectionString"]);
            });
2
2/24/2017 10:23:27 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow