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

2
  • See my question, how to dynamically change connection string, might be helpful. Commented Nov 30, 2017 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. Commented Nov 30, 2017 at 17:43

2 Answers 2

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.
    
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.