How to get the data from two different connection string using same context class

Anjali Agarwal 1,531 Reputation points
2023-08-15T17:49:17.31+00:00

I have the following connection string in my appsettings.json file:

{

"ConnectionStrings": {

"DefaultConnection": "Server=testServer;TrustServerCertificate=True;Initial Catalog=test;uid=test;pwd=test;"

},

"Logging": {

"LogLevel": {

  "Default": "Information",

  "Microsoft.AspNetCore": "Warning"

}

},

"AllowedHosts": "*"

}

This is what I have in my Program.cs file:

using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllersWithViews();

builder.Services.AddDbContext<test.Models.testContext>(options =>

{

options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));

});

The above connection string works fine and I can get the data using entityframework core. I need to get some data from a different database and same server using the same context class. I defined that connection string in my appsettings.json file like this:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=test;TrustServerCertificate=True;Initial Catalog=test;uid=test;pwd=test;"
    "DefaultConnection1": "Server=test1;TrustServerCertificate=True;Initial Catalog=test1;uid=EmpRecUser;pwd=test1;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

How can I get the data from this different connection string using the same context. I have the following context class:

using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace test.Models;

public partial class testContext : DbContext
{
    public testContext ()
    {
    }

    public testContext (DbContextOptions<testContext > options)
        : base(options)
    {
    }
   public virtual DbSet<Category> Categories { get; set; }

Developer technologies | .NET | Entity Framework Core
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-08-15T18:34:23.75+00:00

    the context does not support two databases even if the same server. you need two dbcontext instances initialized with different connect strings. you can either use an injected factory, or have the second inherit from the first to have two type.

    as you are using sqlserver, you can create a view in the primary database that selects from the secondary. you could than add a dbset for this view(s). this has the advantage of cross database joins being done by sqlserver rather than the client.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.