The type or namespace name 'OpenIddictDbContext<,,>' could not be found

血红的双手。 提交于 2019-12-06 05:12:13

问题


I have a problem. I opened my project this morning and got the error:

The type or namespace name 'OpenIddictDbContext<,,>' could not be found (are you missing a using directive or an assembly reference?) [netcoreapp1.1]

This error occurred when I restored and built my project. It's strange because I do have "OpenIddict": "1.0.0-*", in my project.json file and I am using the reference: using OpenIddict;

This issue causes problems everywhere in my project because he doesn't seem to recognise "using OpenIddict"

If it helps, this is an example where I got the error (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>
    {

or

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

        protected override void OnModelCreating(ModelBuilder builder)
        {

Here's my 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"
    }
  }
}

It's strange because every project I open in my visual code has this error so I don't think it has to do with my project.


回答1:


Starting with beta2, OpenIddict no longer comes with a dedicated DbContext you can subclass, as this pattern - inherited from ASP.NET Core Identity - proved to be rather impractical.

Instead, you're now encouraged to directly inherit from IdentityDbContext and register the entity sets needed by OpenIddict by calling options.UseOpenIddict() from ConfigureServices:

project.json:

"dependencies": {
  "OpenIddict": "1.0.0-*",
  "OpenIddict.EntityFrameworkCore": "1.0.0-*",
  "OpenIddict.Mvc": "1.0.0-*"
}

Startup:

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

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


来源:https://stackoverflow.com/questions/41122686/the-type-or-namespace-name-openiddictdbcontext-could-not-be-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!