Why would LINQ all the sudden start acting strange with DB connection
This is an o开发者_开发百科dd 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.
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())
{}
}
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.
精彩评论