Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If both connections are specified in the Web.config file, then you should use <a href="http://msdn.microsoft.com/en-us/library/gg679467%28v=vs.103%29.aspx" rel="nofollow">this overload</a> of the DbContext class. All you need to do is pass in the name that's found in the <code>&lt;connectionStrings&gt;</code> record that you're trying to connect to, and it will go into the Web.config file, grab that connection string, and open the connection.</p> <p>An example (using plain localhost with SQLEXPRESS--easily translatable to your process) </p> <pre><code> &lt;connectionStrings&gt; &lt;add name="Connection1" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog = FirstDB;Integrated Security=True" providerName="System.Data.SqlClient;MultipleActiveResultSets=true;" /&gt; &lt;add name="Connection2" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog = SecondDB;Integrated Security=True" providerName="System.Data.SqlClient;MultipleActiveResultSets=true;" /&gt; &lt;/connectionStrings&gt; </code></pre> <p>Then in your DbContext class have the following constructor present:</p> <pre><code>public MyDbContext(string connectionString) : base(connectionString) { } </code></pre> <p>Where <code>connectionString</code> would be either "Connection1" or "Connection2".</p> <p>EDIT: Per the comments, I think that you'll need to create a layer over your DbContext that will simply do the switching for you. You might want to consider the following steps in doing so:</p> <p>1) Create a class (let's call it ContextLayer) that has on it a DbContext object. Whether or not this class is static is up to you. You also may want to have it implement IDisposable.</p> <p>2) In your Web.Config file for that project be sure to have a connection string entry for all the databases that you would possibly connect to.</p> <p>3) Have a method on the layer class that will give you the DbContext object, using the first connection string found in your config file. As an alternative, you could also make your layer into a repository itself that will have the DbContext object as a private field, and just expose the repository methods for grabbing specific information. </p> <p>4) In this layer class, have a SaveChanges() method that returns an int, just like DbContext.SaveChanges() does. Inside SaveChanges() check to see if the connection is good. Otherwise, go through each of the other connection string entries in the Web.Config file and test them out, using a new instance of the DbContext class (a different object than the one on the class currently). If one of them works, do all of the logic for putting the changes on the other DbContext class. If you've decided to make this a static class, you might want to set the DbContext object on the class to this context, depending on your needs.</p> <p>5) If none of the contexts work, maybe have the method return -1, and pop up on the screen that no saving could be done since no databases could be found to save to.</p> <p>By doing this coding once you will guarantee that every time you try to save to a database, you only need to make 1 very simple call to the layer class to do so, and that even if it doesn't work, you get a nice, clean way to show to the client that it couldn't connect.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload