简体   繁体   English

ASP.NET Core:在使用自定义 DbContext 的同时也使用 Identity 时出错

[英]ASP.NET Core: Error when using a custom DbContext while also using Identity

I've created a dbContext based on an existing Azure SQL Database using Entity Framework.我已经使用实体框架基于现有的 Azure SQL 数据库创建了一个 dbContext。 I added this database to my app's services as follows:我将此数据库添加到我的应用程序的服务中,如下所示:

        public void ConfigureServices(IServiceCollection services)
    {
        //Identity Database Context
        services.AddDbContext<IdentityDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DataDb"),
            optionsBuilders =>
            optionsBuilders.MigrationsAssembly("WebPortal"))
        );

        services.AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<IdentityDbContext>()
            .AddDefaultTokenProviders();

        //Custom Database Context
        services.AddDbContext<CustomDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("CustomDb"))
        );

        services.AddMvc();

    }

When I try to run this I get the following error message:当我尝试运行它时,我收到以下错误消息:

InvalidOperationException: The DbContextOptions passed to the IdentityDbContext constructor must be a DbContextOptions. InvalidOperationException:传递给 IdentityDbContext 构造函数的 DbContextOptions 必须是 DbContextOptions。 When registering multiple DbContext types make sure that the constructor for each context type has a DbContextOptions parameter rather than a non-generic DbContextOptions parameter.注册多个 DbContext 类型时,请确保每个上下文类型的构造函数都有一个 DbContextOptions 参数,而不是一个非通用的 DbContextOptions 参数。

The constructor for my custom Context does have a parameter:我的自定义上下文的构造函数确实有一个参数:

    public CustomDbContext(DbContextOptions<CustomDbContext> options)
        : base(options)
    {
    }

Why am I getting the error?为什么我收到错误?

I had the same problem.我有同样的问题。 my scenario was that, i needed two Context ReadDataContext and WriteDataContext ,i solved that exception with bottom Contexts我的情况是,我需要两个 Context ReadDataContextWriteDataContext ,我用底部 Contexts 解决了这个异常

public class ReadOnlyDataContext : DbContext
{
    public ReadOnlyDataContext(DbContextOptions options) : base(options)
    {
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.RemovePluralizingTableNameConvention();
        Assembly assemblyWithConfigurations = typeof(TaskConfiguration).Assembly;
        modelBuilder.ApplyConfigurationsFromAssembly(assemblyWithConfigurations);
    }
}

Pay attention to the DataContext Constructor注意DataContext构造函数

 public class WriteDataContext : DbContext, IContext
{
    public WriteDataContext(DbContextOptions<WriteDataContext> options)
        : base(options)
    {
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.RemovePluralizingTableNameConvention();
        Assembly assemblyWithConfigurations = typeof(TaskConfiguration).Assembly;
        modelBuilder.ApplyConfigurationsFromAssembly(assemblyWithConfigurations);
    }
}

And for registeration和注册

 services.AddDbContext<DataContext>(opt =>
        {
            opt.UseSqlServer(configuration.GetConnectionString("CommanderConnection"));
            opt.LogTo(Console.WriteLine).EnableSensitiveDataLogging();
        });
 services.AddDbContext<ReadOnlyDataContext>(opt =>
        {
            opt.UseSqlServer(configuration.GetConnectionString("CommanderConnection"));
            opt.LogTo(Console.WriteLine).EnableSensitiveDataLogging();
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Scaffold-DbContext时的ASP.NET Core Error - ASP.NET Core Error while using Scaffold-DbContext 在 ASP.NET Core MVC 中将其用于 ActionFilter 时出现 DbContext 错误 - DbContext error when using it for ActionFilter in ASP.NET Core MVC ASP.NET Core(3.0) 上的身份在使用端点时不起作用 - Identity on ASP.NET Core(3.0) not functioning when using endpoints 将 ASP.NET 核心标识与 MongoDB 一起使用 - Using ASP.NET Core Identity with MongoDB 在使用 EF Core 在 ASP.NET Core 中执行 CRUD 时,我应该单独使用 DbContext 还是使用 DbSet - Should I use DbContext alone or DbSet when performing CRUD in ASP.NET Core using EF Core Asp.net身份DbContext - Asp.net Identity DbContext ASP NET CORE 无法使用现有的 DbContext 来搭建 Identity - 如何在搭建时指定选项? - ASP NET CORE Can't scaffold Identity using existing DbContext - how do I specify options when scaffolding? ASP.NET Core中的自定义RoleProvider与身份? - Custom RoleProvider in ASP.NET Core with Identity? 在 asp.net 内核中添加身份迁移时出错 - Error when adding identity migrations in asp.net core 使用 ASP.NET 核心标识时存储用户类型特定属性的最佳实践是什么? - What is the best practice for storing user type specific properties while using ASP.NET Core Identity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM