0

I am absolutely new to ASP.NET Core. I am building a ASP.NET Core web api trial project following official documentation. I wanted to set up PostgresSQL with this project so I did following:

My model:

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

And my 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%" ]
  }
}

However, I get following error in my startup.cs:

enter image description here

I restored packages, but I am basically not even sure what it really means and what has to be done. I did not yet execute: dotnet ef migration

2
  • maybe you should migrate to core 1.1? Commented Feb 23, 2017 at 19:51
  • In that case I need to upgrade everything to 1.1, right? Commented Feb 23, 2017 at 20:04

1 Answer 1

2

As you are using .NET Core 1.0, not .NET Core 1.1, downgrade the version of Npgsql.EntityFrameworkCore.PostgreSQL nuget.

Use "Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.2" instead of "Npgsql.EntityFrameworkCore.PostgreSQL": "1.1.0" in your project.json.

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

1 Comment

Okay, I will try it now. But otherwise all other settings are correct, right? Setting up connection string and using it in startup.cs file and setting up DBContext. I had to follow couple of tutorials as it wasn't part of web api documentation.

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.