views:

61

answers:

2

This is an odd one. All the sudden when I create LINQ to SQL projects in Visual Studio would it not create the following line in the designer, up to three weeks ago or so, it used to create this line, now I have to manually do it. Then if I make changes in the dbml file it removes it and I have to manuallhy add again. huge pain. Here is the lines I am speaking of:

   public DataContext():
                    base(global::System.Configuration.ConfigurationManager.ConnectionString["SiteSqlServer"].ConnectionString, mappingSource)  
        {  
            OnCreated();  
        }  

I am using Visual Studio 2008 sp1.

A: 

It's a known problem.

You have to implement sort of factory for DB context. Otherwise you will always stuggle with the designer and never know which connection you are using.

public partial class CustomDataContext {
    public static CustomDataContext Create()
    {
         return new CustomDataContext(ConfigurationManager.ConnectionStrings["CustomConnectionString"].ConnectionString);
    }
}

Another option is inheriting from your CustomDataContext, and creating a default constructor:

public CustomDataContext : InternalCustomDataContext /* created in designer */
{
 protected string GetCustomConnectionString() {
   return ConfigurationManager.ConnectionStrings["CustomConnectionString"].ConnectionString;
 }
 public CustomDataContext() : base(GetCustomConnectionString())
 {}
}
George Polevoy
Thank you for your insight, but why have I created 50+ projects and this never happened and all the sudden it is constantly happening
James Campbell
Personally i'm more happy with implementing a deterministic strategy to avoid the entire class of problems then learning the steps to avoid this designer behaviour.
George Polevoy
A: 

Look at the connection properties in the linq2sql designer, try the different settings until you find the one that creates that initializes the connection in the context.

I recall receiving that in a project, and as far as I remember its related to the way you make/configure the connection. I fixed it at the time, but I don't recall the exact property/place.

eglasius