类型或名称空间名称'OpenIddictDbContext< ,,>'找不到 [英] The type or namespace name 'OpenIddictDbContext<,,>' could not be found

查看:150
本文介绍了类型或名称空间名称'OpenIddictDbContext< ,,>'找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.今天早上我打开了项目,并收到错误消息:

找不到类型或名称空间名称'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>
    {

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

        protected override void OnModelCreating(ModelBuilder builder)
        {

这是我的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"
    }
  }
}

这很奇怪,因为我在可视代码中打开的每个项目都存在此错误,因此我认为这与我的项目无关.

解决方案

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

现在,建议您直接从IdentityDbContext继承,并通过从ConfigureServices调用options.UseOpenIddict()来注册OpenIddict所需的实体集:

project.json:

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

启动:

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

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.

解决方案

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

这篇关于类型或名称空间名称'OpenIddictDbContext&lt; ,,&gt;'找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆