2

I have created a Web API using .net framework and using entity framework 6.0.

I have multiple connection strings (suppose 2 connection strings are present in web.config) so that if one server is down then Entity Framework should connect to another server using second connection string.

Please suggest how can I achieve this?

Regards, Rashi

Rashi Garg
  • 177
  • 1
  • 1
  • 9
  • You might be able to build a solution around [this](http://stackoverflow.com/questions/20216147/entity-framework-change-connection-at-runtime.) – Steve Greene Jan 14 '17 at 17:28
  • @SteveGreene, Thanks for your input but in my case it will not work because I am using code first approach and in my case entity framework should detect if database server is down then connect to another database server present in web.config. – Rashi Garg Jan 15 '17 at 11:38
  • 1
    Code first doesn't matter - we are talking about contexts and connection strings that both use. Basically `try catch` the primary connection and if it fails change to the second. – Steve Greene Jan 15 '17 at 17:26
  • @SteveGreene, I am following same try catch block but how can I detremine which connection string is valid in Create method implemented from IDbContextFactory interface. – Rashi Garg Jan 16 '17 at 07:49
  • It would help if you posted some code, but essentially compose your context with the first connect string, then do `if (!dbContext.Database.Exists())` to check it. If it fails, try the other. – Steve Greene Jan 16 '17 at 15:46
  • Create context with first conn string as param, try/catch = the db server is down, create context with another connString as param.. – Nick Kovalsky Mar 20 '18 at 07:44

0 Answers0