4

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.

Kévin Chalet
  • 39,509
  • 7
  • 121
  • 131
hxwtch
  • 135
  • 3
  • 14

1 Answers1

7

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);
    }
}
Kévin Chalet
  • 39,509
  • 7
  • 121
  • 131
  • Note: you can find up-to-date samples here: https://github.com/openiddict/openiddict-samples. – Kévin Chalet Dec 13 '16 at 14:57
  • I have the same issue, setting those 3 in project.json errors trying to retrieve OpenIddict.EntityFrameworkCore. It looks like you haven't added the beta versions of OpenIddict – Dave Dec 13 '16 at 15:18
  • @Dave if you want me to help you, you'll have to post more details about the errors you're getting. Otherwise, it's impossible to say what's causing them. – Kévin Chalet Dec 13 '16 at 15:24
  • I add the 3 dependencies above and I get "package restore failed". I needed to add OpenIddict.Core – Dave Dec 13 '16 at 15:30
  • @Dave sounds very unlikely, as `OpenIddict.Core` is referenced by `OpenIddict`, `OpenIddict.EntityFrameworkCore` and `OpenIddict.Mvc`. Referencing it explicitly shouldn't be necessary. – Kévin Chalet Dec 13 '16 at 15:32
  • Thank you. This worked for me but I did have to comment out "builder.UseOpenIddict();" in order for it to work – hxwtch Dec 13 '16 at 15:37
  • I used the following, OpenIddict and OpenIddict.Mvc brought down the alpha versions when using 1.0.0.* '"OpenIddict": "1.0.0-beta2-0509", "OpenIddict.EntityFrameworkCore": "1.0.0-beta2-0509", "OpenIddict.Mvc": "1.0.0-beta2-0509",' – Dave Dec 13 '16 at 15:38
  • @sds if you don't register the OpenIddict entity sets, it won't work properly. What was wrong with `builder.UseOpenIddict()`? – Kévin Chalet Dec 13 '16 at 15:39
  • it gives the error: 'ModelBuilder' does not contain a definition for 'UseOpenIddict' and no extension method 'UseOpenIddict' – hxwtch Dec 13 '16 at 16:45
  • Can you tell me more about your environment? The VS version you're using. Your project.lock.json, etc. – Kévin Chalet Dec 13 '16 at 16:49
  • Note: it looks like you're not the only one having issues using the `builder.UseOpenIddict()` extension. Can you join us at https://gitter.im/openiddict/openiddict-core and share more details? – Kévin Chalet Dec 13 '16 at 17:09