具有2个数据库的实体框架核心(aspnetboilerplate webapp)

问题描述 投票:1回答:1

我正在使用Abp.ZeroCore.EntityFrameworkCore 4.1.0并且我正在尝试将另一个上下文(UnoDbContext)添加到我的Web应用程序中,如下所示:

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

这是我的代码。

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

默认上下文:

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

现在,当我尝试在服务中使用UnoDbContext时,我收到此错误:

处理请求时发生未处理的异常。 HandlerException:无法创建组件'HealthCareCRM.EntityFrameworkCore.UnoDbContext',因为它具有要满足的依赖项。

'HealthCareCRM.EntityFrameworkCore.UnoDbContext'正在等待以下依赖项: - 服务'Microsoft.EntityFrameworkCore.DbContextOptions`1 [[HealthCareCRM.EntityFrameworkCore.UnoDbContext,HealthCareCRM.EntityFrameworkCore,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null] ]'没有注册。

Castle.MicroKernel.Handlers.DefaultHandler.AssertNotWaitingForDependency()

有人能告诉我错误在哪里吗?

c# sql-server entity-framework-core aspnetboilerplate
1个回答
0
投票

看起来你的新DbContext对ABP无效。看看这个示例DbContext。自定义它并将其用作第二个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)
        {

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.