0

I basically have two different databases that have different tables in them, but they both have "Table1" with the exact same structure.

var db = new ???;
if(mode == "PRODUCTION"){
    db = new Database1("Connection string for Database1");
}
else{
    db = new Database2("Connection string for Database2");
}

var result = db.Table1.Where(a=>a.Value==1).First();

How can I make the above work so I can assign "result" from two different databases (depending on "mode") without writing two different definitions for "result"?

2
  • 1
    If you find a solution it won't be safe, as the structures for these tables can change independently. Commented Mar 20, 2013 at 20:55
  • Can you share the classes for Database1 and Database2? Commented Mar 20, 2013 at 20:59

2 Answers 2

3

Store the connection strings separately in your config file. You can then swap the connection strings at runtime to point the database object to the right database:

if(mode == "PRODUCTION")
{
    db = new Database(ConfigurationManager.ConnectionStrings["production-key"]);
}
else
{
    db = new Database(ConfigurationManager.ConnectionStrings["dev-key"]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

My (limited) understanding of linq-to-sql tells me that you need to specify the type of database being instantiated. So like var db = db1(connectionString) will require a db1 specific connection string. Am I wrong?
@sooprise - That's no the case. If the databases have the same schema, you can use the same type and just swap connection strings.
By "same schema" do you mean two databases with exactly the same tables all with the same structure?
@sooprise it will work up until the point that you try to access a table that doesn't exist or has a different schema than expected.
Got it got it, this is cool, I'm trying it now. Will "Check" solution once I get my code working. Thanks everyon.
2

Justin's point is a good one, here is another -

Why do this at the db level?

Something like this would work

var table;

if(mode == "PRODUCTION"){
    db = new Database1("Connection string for Database1");
    table = db.table1;
}
else{
    db = new Database1("Connection string for Database2");
    table = db.table1
}

var result = table.Where(a=>a.Value==1).First();

If you don't have the same exact db then you would need to do something like this (you could also add an interface to db1 and db2 to return commonElements -- as you wish.

class commonElements {
   /// some code
}

public commoneElements GetCommon(Database1 inDB1) {
   /// some code
}

public commoneElements GetCommon(Database2 inDB2) {
   /// some code
}

commonElements common;
if(mode == "PRODUCTION"){
    db1 = new Database1("Connection string for Database1");
    common = GetCommon(db1);
}
else{
    db2 = new Database2("Connection string for Database2");
    common = GetCommon(db2);
}

var result = common.Where(a=>a.Value==1).First();

2 Comments

var table; is not valid. What type is table? The two datacontexts will have different classes for table1, even if the definitions are the same.
@Blorgbeard - yes point taken, I like Justin was assuming the same object type was used for both dbs if they are the same. If not then a common accessor function would need to be implemented see edit

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.