找不到类型或命名空间名称"OpenIddictDbContext <,,>"

hxw*_*tch 4 c# asp.net-mvc visual-studio asp.net-core openiddict

我有个问题.我今天早上打开了我的项目并得到了错误:

找不到类型或命名空间名称'OpenIddictDbContext <,,>'(您是否缺少using指令或程序集引用?)[netcoreapp1.1]

我恢复并构建项目时发生此错误.这很奇怪,因为我在我的project.json文件中有"OpenIddict":"1.0.0-*",我正在使用引用:using OpenIddict ;

这个问题在我的项目中到处都会出现问题,因为他似乎没有认识到"使用OpenIddict"

如果它有帮助,这是我得到错误的例子(ApplicationDbContext.cs)

 namespace Overnight.Db
{
    //the error: The type or namespace name 'OpenIddictDbContext<,,>' could not be found (are you missing a using directive or an assembly reference?)

    public class ApplicationDbContext : OpenIddictDbContext<ApplicationUser, ApplicationRole, Guid>
    {
Run Code Online (Sandbox Code Playgroud)

要么

 //the error: 'OpenIddictDbContext<ApplicationUser, ApplicationRole, Guid>' does not contain a constructor that takes 1 arguments

        protected override void OnModelCreating(ModelBuilder builder)
        {
Run Code Online (Sandbox Code Playgroud)

这是我的project.json:

{
  "version": "1.0.0-*",
  "buildOptions": {
      "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.1.0"
    }, 
    "Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final",
    "AspNet.Security.Oauth.Validation": "1.0.0-alpha2-final",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "OpenIddict": "1.0.0-*",
    "Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.1-*",
    "Npgsql.EntityFrameworkCore.PostgreSQL.Design": "1.0.1-*",
    "Bogus": "7.1.6",
    "Overnight.Models": {
      "target": "project",
      "version": "1.0.0-*"
    }
  },
  "frameworks": {
    "netcoreapp1.1": {}
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": {
        "version": "1.0.0-preview2-final"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为我在可视代码中打开的每个项目都有这个错误,所以我不认为它与我的项目有关.

Kév*_*let 7

从beta2开始,OpenIddict不再带有专用的DbContext子类,因为这种模式 - 继承自ASP.NET核心标识 - 被证明是相当不切实际的.

相反,你现在鼓励直接继承IdentityDbContext调用和注册由OpenIddict所需的实体集options.UseOpenIddict()ConfigureServices:

project.json:

"dependencies": {
  "OpenIddict": "1.0.0-*",
  "OpenIddict.EntityFrameworkCore": "1.0.0-*",
  "OpenIddict.Mvc": "1.0.0-*"
}
Run Code Online (Sandbox Code Playgroud)

启动:

services.AddDbContext<ApplicationDbContext>(options =>
{
    // Configure the context to use Microsoft SQL Server.
    options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"]);

    // Register the entity sets needed by OpenIddict.
    // Note: use the generic overload if you need
    // to replace the default OpenIddict entities.
    options.UseOpenIddict();
});

// Register the OpenIddict services.
services.AddOpenIddict(options =>
{
    // Register the Entity Framework stores.
    options.AddEntityFrameworkCoreStores<ApplicationDbContext>();
});
Run Code Online (Sandbox Code Playgroud)

ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions options)
        : base(options) { }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}
Run Code Online (Sandbox Code Playgroud)