Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Using multiple databases #3590


User avatar
0
hjpetzer created

Dear Support,

I have the need to read/write to multiple databases with Entity Framework Core. I've search through this forums and read through the documentation, but still need some guidance ons best practises and a bit of support.

So for starters, just to ty and get it working, here is what I did.

  1. I've created a new DbContext in *.EntityFrameworkCore project:
public partial class AbpDpmDbContext : DpmDbContextBase
    {
        public AbpDpmDbContext(DbContextOptions<DpmDbContext> options) : base(options)
        {
        }
    }

This extends DpmDbContextBase that is in one for our NuGet packages. I've updated DpmDbContextBase to extend AbpDbContext and implement OnModelCreating:

public abstract class DpmDbContextBase : AbpDbContext
    {
        protected DpmDbContextBase(DbContextOptions options) : base(options)
        {
        }

        public virtual DbSet<Client> Clients { get; set; }
        ....
        <more DbSets>
        ....

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
           modelBuilder.Entity<Client>(entity =>
            {
                 ...
  1. In the application layer, I've created a basic service and a repository for one of the entities.

At this point, If I run the host, I can see the new entry point on the swagger interface and the repository is injected successfully, but I get an exception when the actual query is done:

Abp.AbpException: Could not resolve DbContextOptions for .....EntityFrameworkCore.Dpm.AbpDpmDbContext

I guess this has something to do with the wrong connection string. How do I set the DbContextOptions of my new DbContext?

Should I rather create a new module and make PortalWebCoreModule depend on it?

Thanks in advance for any help.


2 Answer(s)
  • User Avatar
    0
    hjpetzer created

    I've now implemented a separate project with a module and configured the db context in the PreInitialize() method. This is now working, so non issue :)

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks for the feedback @hjpetzer, it is great :)