none
how to manage multiple linq to sql connections RRS feed

  • Question

  • If I create two connections to databases with linq 2 sql. One db is dev the other is production

     

    So I have two datacontext classes, what is the best way to manage both and connect to both when needed?

     

    Should I use preprocessor statements or pass in a connection string to the constructor? Is there a better way?


    chuckdawit
    Tuesday, October 12, 2010 10:39 PM

Answers

All replies

  • What's wrong with passing the connection string? Or better, create a methods that accepts an enum argument (which database) and it returns appropriate context.

    Miha Markic [MVP C#] http://blog.rthand.com
    Wednesday, October 13, 2010 6:37 AM
  • Hi,

    Maybe this post can help you in order to change your connection string dynamically:

    http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/8a89a728-6c8d-4734-98cb-11b196ba11fd

    Best regards,

    JAReyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    Wednesday, October 13, 2010 7:06 AM
  • Hi,

    Do you have any examples of your second option?


    chuckdawit
    Wednesday, October 13, 2010 9:36 PM
  • public partial class YourDataContext()
    {
          public static YourDataContext CreateNew()
          {
               string connectionString = "production";
               #if DEBUG
               connectionString = "dev";
               #endif
    
               return new YourDataContext(connectionString);
          }
    }
    
    Friday, October 15, 2010 9:06 PM
  • my app.config file generated a connectionstring element that looks like this below. How do I select the connection string that I want to use in the code where I pass in the connection string into the DataContext constructor

    DataContext db = new DataContext(connectionString);

     

    <connectionStrings>

        <add name="FileWatcher.Properties.Settings.ImagingConnectionString"

            connectionString="Data Source=Dev;Initial Catalog=Imag;Integrated Security=True"

            providerName="System.Data.SqlClient" />

        <add name="FileWatcher.Properties.Settings.ImagingConnectionString1"

            connectionString="Data Source=prod;Initial Catalog=Imag;Integrated Security=True"

            providerName="System.Data.SqlClient" />

      </connectionStrings>


    chuckdawit
    Friday, October 15, 2010 9:29 PM
  • If you want to read your config file you can use the following link. You can still use the previous CreateNew static method, but read the config from the configuration.

    http://msdn.microsoft.com/en-us/library/system.configuration.configuration.connectionstrings.aspx

    So instead of YourDataContext dc = new YourDataContext();. you can do this YourDataContext dc = YourDataContext.CreateNew();

    Friday, October 15, 2010 10:47 PM