【问题标题】:Setting up ASP.NET Core with PostgreSQL error使用 PostgreSQL 错误设置 ASP.NET Core
【发布时间】:2018-07-30 09:59:01
【问题描述】:

我对 ASP.NET Core 完全陌生。我正在按照官方文档构建一个 ASP.NET Core web api 试用项目。我想用这个项目设置 PostgresSQL,所以我做了以下操作:

我的模特:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api_trial.Models
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Grade { get; set; }
    }

    public class WebAPIDataContext : DbContext
    {
        public WebAPIDataContext(DbContextOptions<WebAPIDataContext> options)
            : base(options)
        {
        }
        public DbSet<Student> Students { get; set; }
    }
}

appsettings.json:

{
  "ConnectionStrings": {
    "DataAccessPostgreSqlProvider": "User ID=postgres;Password=root;Host=localhost;Port=5432;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(Configuration.GetConnectionString("DataAccessPostgreSqlProvider"));
            });

            services.AddMvc();
            services.AddSwaggerGen();
        }

还有我的 project.json:

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Routing": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "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.1",
    "Microsoft.EntityFrameworkCore.InMemory": "1.0.1",
    "Swashbuckle": "6.0.0-beta902",
    "Npgsql.EntityFrameworkCore.PostgreSQL": "1.1.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.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%" ]
  }
}

但是,我的 startup.cs 中出现以下错误:

我恢复了软件包,但基本上我什至不确定它的真正含义以及必须做什么。我还没有执行:dotnet ef migration

【问题讨论】:

  • 也许您应该迁移到核心 1.1?
  • 在那种情况下,我需要将所有东西都升级到 1.1,对吧?

标签: postgresql asp.net-core asp.net-core-mvc


【解决方案1】:

由于您使用的是 .NET Core 1.0,而不是 .NET Core 1.1,请降级 Npgsql.EntityFrameworkCore.PostgreSQLnuget 的版本。

在您的 project.json 中使用 "Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.2" 而不是 "Npgsql.EntityFrameworkCore.PostgreSQL": "1.1.0"

【讨论】:

  • 好的,我现在就试试。但除此之外所有其他设置都是正确的,对吧?设置连接字符串并在 startup.cs 文件中使用它并设置 DBContext。我必须遵循几个教程,因为它不是 web api 文档的一部分。
猜你喜欢
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多