EF Core與Postgres連接字符串錯誤

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

我第一次嘗試使用ASP.net Core和postgres。我正在關注( http://dotnetthoughts.net/using-postgresql-with-aspnet-core/ )此博文,以創建我的第一次遷移。我已經在我的posgresSQL中創建了一個名為asp_api_test的數據庫。

我的dotnet ef migrations add Initial成功。但是當我運行dotnet ef database update遇到了錯誤Format of the initialization string does not conform to specification starting at index 0

我的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"
    }
  }
}

我猜我的連接字符串不正確。我的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();
        }

我的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%" ]
  }
}

我究竟做錯了什麼?

熱門答案

我更新到以下 -

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

在我的startup.cs中:

services.AddDbContext<WebAPIDataContext>(options => {
                options.UseNpgsql(Configuration["Data:DefaultConnection:ConnectionString"]);
            });


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow