1

I'm using Abp.ZeroCore.EntityFrameworkCore 4.1.0 and I'm trying to add another context (UnoDbContext) to my web app, like this:

https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core#replacing-default-repositories

This is my code.

UnoDbContext.cs:

using Microsoft.EntityFrameworkCore;
using MyApp.Models;
using Abp.EntityFrameworkCore;

namespace MyApp.EntityFrameworkCore
{
    public class UnoDbContext : AbpDbContext
    {
        /* Define a DbSet for each entity of the application */
        public virtual DbSet<Anagrafica> Anagraficas { get; set; }

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

UnoDbContextConfigurer:

using System.Data.Common;
using Microsoft.EntityFrameworkCore;

namespace MyApp.EntityFrameworkCore
{
    public static class UnoDbContextConfigurer
    {
        public static void Configure(DbContextOptionsBuilder<UnoDbContext> builder, string connectionString)
        {
            builder.UseSqlServer(connectionString);
        }

        public static void Configure(DbContextOptionsBuilder<UnoDbContext> builder, DbConnection connection)
        {
            builder.UseSqlServer(connection);
        }
    }
}

Startup.cs:

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Castle.Facilities.Logging;
using Abp.AspNetCore;
using Abp.Castle.Logging.Log4Net;
using MyApp.Authentication.JwtBearer;
using MyApp.Configuration;
using MyApp.Identity;
using MyApp.Web.Resources;
using Abp.AspNetCore.SignalR.Hubs;
using Abp.EntityFrameworkCore;
using MyApp.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace MyApp.Web.Startup
{
    public class Startup
    {
        private readonly IConfigurationRoot _appConfiguration;

        public Startup(IHostingEnvironment env)
        {
            _appConfiguration = env.GetAppConfiguration();
        }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            // MVC
            services.AddMvc(
                options => options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute())
            );

            IdentityRegistrar.Register(services);
            AuthConfigurer.Configure(services, _appConfiguration);

            services.AddScoped<IWebResourceManager, WebResourceManager>();

            services.AddSignalR();

            services.AddAbpDbContext<UnoDbContext>(options =>
            {
                options.DbContextOptions.UseSqlServer(options.ConnectionString);
            });

            // Configure Abp and Dependency Injection
            return services.AddAbp<MyAppWebMvcModule>(
                // Configure Log4Net logging
                options => options.IocManager.IocContainer.AddFacility<LoggingFacility>(
                    f => f.UseAbpLog4Net().WithConfig("log4net.config")
                )
            );
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseAbp(); // Initializes ABP framework.

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();
            app.UseAuthentication();
            app.UseJwtTokenMiddleware();

            app.UseSignalR(routes =>
            {
                routes.MapHub<AbpCommonHub>("/signalr");
            });

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "defaultWithArea",
                    template: "{area}/{controller=Home}/{action=Index}/{id?}");

                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

default context:

using Microsoft.EntityFrameworkCore;
using Abp.Zero.EntityFrameworkCore;
using MyApp.Authorization.Roles;
using MyApp.Authorization.Users;
using MyApp.MultiTenancy;
using MyApp.Models;

namespace MyApp.EntityFrameworkCore
{
    public class MyAppDbContext : AbpZeroDbContext<Tenant, Role, User, MyAppDbContext>
    {
        /* Define a DbSet for each entity of the application */

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

Now when I try to use the UnoDbContext in a service I get this error:

An unhandled exception occurred while processing the request. HandlerException: Can't create component 'HealthCareCRM.EntityFrameworkCore.UnoDbContext' as it has dependencies to be satisfied.

'HealthCareCRM.EntityFrameworkCore.UnoDbContext' is waiting for the following dependencies: - Service 'Microsoft.EntityFrameworkCore.DbContextOptions`1[[HealthCareCRM.EntityFrameworkCore.UnoDbContext, HealthCareCRM.EntityFrameworkCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' which was not registered.

Castle.MicroKernel.Handlers.DefaultHandler.AssertNotWaitingForDependency()

Can someone tell me where the error is?

2
  • You need a reference to Microsoft.EntityFrameworkCore.DbContextOptions and most likely also to Microsoft.EntityFrameworkCore Commented Mar 19, 2019 at 18:50
  • 1
    @Aldert how? it already contains an the original context and it works, that i suppose the references are already present Commented Mar 20, 2019 at 9:21

1 Answer 1

1

looks like your new DbContext is invalid for ABP. Check out this sample DbContext. Customize it and use it as your second DbContext.

using Abp.Zero.EntityFrameworkCore;
using Abp.Zero.SampleApp.MultiTenancy;
using Abp.Zero.SampleApp.Roles;
using Abp.Zero.SampleApp.Users;
using Microsoft.EntityFrameworkCore;

namespace Abp.Zero.SampleApp.EntityFrameworkCore
{
    public class AppDbContext : AbpZeroDbContext<Tenant, Role, User, AppDbContext>
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) 
            : base(options)
        {

        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I don't see a second DbContext in your code. You just use UnoDbContext
I use the default context of aspnetboilerplate also ( i ve added it in the question above )

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.