IServiceCollection不包含AddNpgsql的定義或擴展名

asp.net asp.net-core entity-framework-core postgresql

我正在嘗試第一次使用PostgreSQL,並且在沒有錯誤的情況下讓它構建時有點麻煩,除非我鎖定RC2包,這很好。鎖定到RC2包後,我有一個錯誤,我似乎無法動搖,我可以使用幫助故障排除:

錯誤

Startup.cs(22,18): error CS1061: 'IServiceCollection' does not contain a definition for 'AddNpgsql' and no extension method 'AddNpgsql' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

PROJECT.JSON

{
  "buildOptions": {
    "preserveCompilationContext": true,
    "emitEntryPoint": true,
    "warningsAsErrors": true,
    "debugType": "portable",

  "copyToOutput": {
      "include": [
        "wwwroot",
        "Views",
        "config.json",
        "web.config"
      ]
    }
  },

  "dependencies": {
    "AspNet.Security.OAuth.Introspection": "1.0.0-alpha1-final",
    "AspNet.Security.OAuth.Validation": "1.0.0-alpha1-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc.Formatters.Json": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc.Cors": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",

    "Microsoft.Extensions.Configuration.CommandLine": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",

    "Microsoft.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.Commands": "1.0.0-rc2-*",
    "Microsoft.EntityFrameworkCore.Relational": "1.0.0-rc2-final",
    "Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.0-*",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-final",

    "OpenIddict.Core": "1.0.0-*",
    "OpenIddict.EF": "1.0.0-*"
  },


  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0-rc2-3002702"
        }
      },

      "imports": [
        "dnxcore50",
        "portable-net451+win8"
      ]
    }
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+wp80+win8+wpa81+dnxcore50"
    }
  },

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

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "config.json",
      "web.config"
    ]
  }
}

STARTUP.CS(部分)

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddEntityFramework()
        .AddNpgsql()
        .AddDbContext<ApplicationContext>(options =>
            options.UseNpgsql("Host=localhost;Username=dev;Password=dev;Database=tesdb"));

    services.AddIdentity<tbl_ApplicationUser, tbl_ApplicationRole>()
        .AddEntityFrameworkStores<ApplicationContext>()
        .AddUserStore<CustomStore>()
        .AddDefaultTokenProviders()
        .AddOpenIddictCore<tbl_Application>
            (conf => conf.UseEntityFramework());

    services.AddAuthorization(options => {
        options.AddPolicy("Default", builder => {
            builder.RequireAuthenticatedUser();
            builder.RequireActiveUser();
        });

        options.DefaultPolicy = options.GetPolicy("Default");
    });

    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<IDatabaseInitializer, DatabaseInitializer>();
    services.AddTransient<IUtilityService, UtilityService>();
    services.AddScoped<ICommonRepository, CommonRepository>();
}

一般承認的答案

RC2驅動程序用AddNpgsql替換AddEntityFrameworkNpgsql ,所以替換此代碼塊:

services.AddEntityFramework()
    .AddNpgsql()
    .AddDbContext<ApplicationContext>(options =>
        options.UseNpgsql("Host=localhost;Username=dev;Password=dev;Database=tesdb"));

有了這個:

services.AddEntityFrameworkNpgsql()
    .AddDbContext<ApplicationContext>(options => 
        options.UseNpgsql("Host=localhost;Username=dev;Password=dev;Database=tesdb"));

熱門答案

在EF Core 1.1中,您需要使用以下代碼:

public class Startup
{
    //...

    public void ConfigureServices(IServiceCollection services)
    {
       //...
       services.AddEntityFrameworkNpgsql();
    }
}

public class DatabaseClass : DbContext 
{
    //...

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql("my connection string");
    }
}


Related

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