Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 256
hxwtch Avatar asked Mar 11 '23 10:03

hxwtch


1 Answers

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);
    }
}
like image 178
Kévin Chalet Avatar answered Apr 28 '23 03:04

Kévin Chalet