views:

30

answers:

1

I've several databases that contains the exact same base tables with identical design. Now I need to be able to access a base table from any one of those database.

Is there any way to create a common interface that can still utilize the power of Linq2Sql? I was thinking that I would have a factory that created a data context for that chosen database which I could afterwards query:

string university = "SomeUniversity";
var database = UniversityDataContextFactory.Get(university);
var user = database.Users.Where(u => u.Id== id).First();

This, however, would require the data contexts returned from the individual databases to implement a shared interface and very possibly also to share data classes.

A: 

If the database schemas are identical then you only need one set of data classes - the only difference between one "data context" and another will be the connection string.

This is where things get a bit more fun. If you are explicitly creating the data context - as in your factory example - then there's not an issue so long as you have a means to provide/create the appropriate connection sting as there is a constructor for a data context that takes a connection string as a parameter. However if you're implicitly creating one (in back of something like dynamic data) then I'm not sure what you'd need to do (I've dealt with this on a per application instance basis but not for multiple connections in a single application).

The key to remember is that the data connection string in your .config is the default but not necessarily the only connection string that you can use with a data context.

Murph