3

I am developing webapi with some knowledge of Core 2.0 I need to create two link to produce different result from different database both database schema are the same.

Like https://localhost:5000/m/University
or https://localhost:5000/v/University
They both fire in different database with same webapi application and give result it. I can produced result but I have to either create two webapi project or change connection string ("MConn") in Start up class.


  public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<IISOptions>(options => { options.AutomaticAuthentication = true; });
        services.AddDbContext<PIDBContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("MConn")));
        services.AddMvc();
    }

Could you help me how to use same Webapi project with different connection string based on Https request?

Thanks

PantherUSA
  • 99
  • 2
  • 8
  • See [my question, how to dynamically change connection string](https://stackoverflow.com/questions/36816215/dynamically-change-connection-string-in-asp-net-core), might be helpful. – Yurii N. Nov 30 '17 at 17:15
  • Thanks for reply but I have at least 50 controller with different result produce with those controller route. That means need to change lot. – PantherUSA Nov 30 '17 at 17:43

2 Answers2

3

Found the solution:

  1. Create DbContextFactory

    public static class DbContextFactory
    {
        public static Dictionary<string, string> ConnectionStrings { get; set; }
    
        public static void SetConnectionString(Dictionary<string, string> connStrs)
        {
            ConnectionStrings = connStrs;
        }
    
        public static AppContext Create(string connid)
        {
            if (!string.IsNullOrEmpty(connid))
            {
                var connStr = ConnectionStrings[connid];
                var optionsBuilder = new DbContextOptionsBuilder<AppContext >();
                optionsBuilder.UseSqlServer(connStr);
                var db = new AppContext (optionsBuilder.Options);
                return db;
            }
            else
            {
                throw new ArgumentNullException("Connection failed becuase of no Connection ID");
            }
       } 
    }
    
  2. Project Json file:

    {
        "ConnectionStrings": {
        "Sample1con": "your 1st connection string",
        "Sample2con": "your 2nd connection string"
        }
    }
    
  3. In Configure method in Startup class declare Dictionary

    Dictionary<string, string> connStrs = new Dictionary<string, string>();
    connStrs.Add("p", Configuration.GetConnectionString("Sample1con"));
    connStrs.Add("m", Configuration.GetConnectionString("Sample2con"));
    DbContextFactory.SetConnectionString(connStrs);
    
  4. In action usage

    var _context = DbContextFactory.Create(db); ///Db would be your URL string.
    
Neuron
  • 5,141
  • 5
  • 38
  • 59
PantherUSA
  • 99
  • 2
  • 8
1

Do you have to have two links? If not, can't you achieve that by doing following.

Nishith Shah
  • 313
  • 1
  • 3
  • 11
  • This is nice way to approach if only small change in few controller. I have 50 or more controller and multiple method as well. So There would be lots of duplicate code. Is this only option? – PantherUSA Nov 30 '17 at 20:12