views:

57

answers:

3

How can we change the underlying database for Linq based WebApp ?

for example:

When we release our web application, say to release from production, if using ADO.NET, it is as simple as modifying connection string in web.config to point towards the live Database in use. The databases are almost identical, other data stored..

What and how do we change unerLying Database when using LINQ ?

Note ; Using c#

thanks

A: 

If you just want to know how you can move the connection string to the Web.Config Rick Strahl has an example on his blog.

If you want to change to a different type of database... You can probably save most (if not all of the queries.) But you will need to change the provider/repository. LINQ 2 SQL only support MSSQL at this time (and that is all it is really meant to do.)

Check out LINQ to Entities if you need more flexibility.

Matthew Whited
Pretty sure OP is asking about changing the connection string, not the type of database
bdukes
Databases are are of same type (SQL SERVER 2005), The difference is for "production DB" and "Live DB"
Asad Butt
Yeah I saw that after I posted my answer. I added a note to that point.
Matthew Whited
Thanks for the answer, do appreciate
Asad Butt
+4  A: 

You should still be able to do the same; DataContext (including the SqlMetal generated ones) include constructor overloads that accept a connection string. Just fetch the string you want from ConfigurationManager (or whatever) and pass it in.

Actually, if you don't mind hacking the DBML directly, it does actually support reading from ConfigurationManager directly; the problem is that the IDE designer hates this, and breaks it every time you look at it. Which is a shame (discussed here).

Marc Gravell
Thanks mate, do appreciate,
Asad Butt
+1  A: 

Hey,

I would highly recommend in your app instantiating the data context with the connection string overload. I create a helper component that does:

public static class DCHelper
{
  public static MyDataContext Create()
  {
     return new MyDataContext(ConfigurationManager.ConnectionStrings["CS"].ConnectionString);
  }
}

That way, I only have one place to change the way the context is instantiated, or I can change the config file to switch databases. Or, the create method can take some input to determine the connection.

Brian
Thanks mate, job done
Asad Butt