0

I'm building a Linq-to-SQL page as an experiment. I'm invoking stored procedures through Linq-to-SQL. The designer.cs file that gets created uses the wrong DataSource and connection string. I don't see any place to set these default values.

Right now I'm doing something stupid - I go in and manually change those two lines every time I modify the .dbml file.

I thought, perhaps, I could do something like this:

using (TEMPDataContext dbContext = new TEMPDataContext("MyConnectionString"))
{
     ....
}

But that doesn't work.

Also, I'm wondering about this line from the designer.cs file:

[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="DataSource")]

Since the database is supplied in the connection string, why is this needed?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
elbillaf
  • 1,952
  • 10
  • 37
  • 73

2 Answers2

0

When you create your .dbml file, you should have to enter the connection string information and then it will automatically add the connection string to your web config. If somehow this got messed up then just drop and recreate the .dbml file.

If for some reason you can't drop and recreate then figure out which connection string it is using and either modify it or create another connection string and then do a find and replace on the other connection string to your new connection string.

Ben
  • 1,820
  • 2
  • 14
  • 25
0

Ah...I figure out how to do this in a non-stupid way. There was actually a SO topic explaining this, but I misread it:

private string connectionString = 
System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

.
.
.
using (TEMPDataContext dbContext = new TEMPDataContext(connectionString))
{
     ....
}

Here's a reference link: LINQ to SQL connectionstring

His solution defines a key (variable) in the web.config which I prefer not to do. Other than that, I'm doing the same thing. It worked.

elbillaf
  • 1,952
  • 10
  • 37
  • 73