2017-02-23 67 views
0

我绝对是新的ASP.NET核心。我正在按照官方文档构建一个ASP.NET Core web api试用项目。我想成立PostgreSQL的这个项目,所以我没有以下内容:设置ASP.NET核心与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以下错误:

enter image description here

我恢复软件包,但我基本都不知道它的真正含义,什么必须是完成。我还没有执行:dotnet ef migration

+0

也许你应该迁移到核心1.1? – Alexan

+0

在这种情况下,我需要将所有内容升级到1.1,对吧? – Nitish

回答

2

由于您使用的是.NET Core 1.0,而不是.NET Core 1.1,因此请将Npgsql.EntityFrameworkCore.PostgreSQL nuget的版本降级。

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

+0

好的,我现在就试试。但是否则所有其他设置都是正确的,对吗?设置连接字符串并在startup.cs文件中使用它并设置DBContext。我不得不遵循几个教程,因为它不是web api文档的一部分。 – Nitish

+0

@Nitish目前的设置对我来说很好 – Set

+0

好的非常感谢!希望有一个好的开始.net核心:) – Nitish