10

I've created a dbContext based on an existing Azure SQL Database using Entity Framework. 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. 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.

The constructor for my custom Context does have a parameter:

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

Why am I getting the error?

7
  • 2
    Try this public IdentityDbContext (DbContextOptions<IdentityDbContext > options) : base(options) { } Commented Jul 24, 2018 at 22:00
  • @nemke Where should I add that? Commented Jul 24, 2018 at 22:05
  • 2
    Maybe a CustomDbContext should use DbContextOptions<CustomDbContext> instead of DbContextOptions<WITSPeopleContext>? Commented Jul 24, 2018 at 23:28
  • Can you show CustomDbContext? Commented Jul 25, 2018 at 4:47
  • @AlyEl-Haddad my mistake, it is using DbContextOptions<CustomDbContext>, I've updated the question Commented Jul 25, 2018 at 18:00

1 Answer 1

10

I had the same problem. my scenario was that, i needed two Context ReadDataContext and WriteDataContext,i solved that exception with bottom 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

 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();
        });
Sign up to request clarification or add additional context in comments.

1 Comment

Similar situation for me. I had two DbContexts and it was pulling the options from the other one. Changing the default "DbContextOptions" to a generic (more specific) parameter in the constructor solved my problem: DbContextOptions<ApplicationAuditDbContext>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.