Base solution for your next web application
Open Closed

Using multiple DB contexts in core MVC jquery not working #3540


User avatar
0
giel83 created

Hello,

For a client of ours I need to target a few databases to extract and insert information. Now, I've tried creating the following classes in the EntityFrameworkCore project:

  • SecondDbContext.cs
  • SecondDbContextConfigurer.cs
  • SecondDbContextFactory.cs

I've also added the following in the Preinitialize() of the FirstInterfaceEntityFrameworkCoreModule.cs:

//add linkedin robot db context
                Configuration.Modules.AbpEfCore().AddDbContext<SecondDbContext>(configuration =>
                {
                    SecondDbContextConfigurer.Configure(configuration.DbContextOptions, configuration.ConnectionString);
                });

But for some reason when I try to use an appservice for the second database, it says table not found because it is trying to connect with the first database.

I'm not sure what i'm doing wrong here? :( Thanks in advance!


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share your second DbContext definition ?

    Thanks.

  • User Avatar
    0
    giel83 created

    <cite>ismcagdas: </cite> Hi,

    Can you share your second DbContext definition ?

    Thanks.

    Hey, thanks for your response and sorry for the slow reply!

    My second DB Context looks as following:

    public class SecondDbContext : AbpDbContext
        {
            public virtual DbSet<tblRelatie> tblRelatie { get; set; }
    
    
            public SecondDbContext(DbContextOptions<SecondDbContext> options)
                : base(options)
            {
            }
        }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Sorry, I have missed this somehow. You need to provide connectionString for your second db here

    Configuration.Modules.AbpEfCore().AddDbContext<SecondDbContext>(configuration =>
    {
        SecondDbContextConfigurer.Configure(configuration.DbContextOptions, "ConnectionStringOfSecondDbContext here...");
    });
    

    Then it should work.

    Thanks.

  • User Avatar
    0
    giel83 created

    Hey ismcagdas,

    Thanks a lot :). This works great!

    Greetings,

  • User Avatar
    0
    alper created
    Support Team

    Thanks for your feedback ;)