Can I stop the dbml designer from adding a connection string to the dbml file?

Posted by drs9222 on Stack Overflow See other posts from Stack Overflow or by drs9222
Published on 2010-04-19T12:17:40Z Indexed on 2010/04/19 15:43 UTC
Read the original article Hit count: 293

We have a custom function AppSettings.GetConnectionString() which is always called to determine the connection string that should be used. How this function works is unimportant to the discussion. It suffices to say that it returns a connection string and I have to use it.

I want my LINQ to SQL DataContext to use this so I removed all connection string informatin from the dbml file and created a partial class with a default constructor like this:

public partial class SampleDataContext
{
    public SampleDataContext() : base(AppSettings.GetConnectionString()) { }
}

This works fine until I use the designer to drag and drop a table into the diagram. The act of dragging a table into the diagram will do several unwanted things:

  • A settings file will be created
  • A app.config file will be created
  • My dbml file will have the connection string embedded in it

All of this is done before I even save the file!

When I save the diagram the designer file is recreated and it will contain its own default constructor which uses the wrong connection string. Of course this means my DataContext now has two default constructors and I can't build anymore!

I can undo all of these bad things but it is annoying. I have to manually remove the connection string and the new files after each change!

Is there anyway I can stop the designer from making these changes without asking?

EDIT

The requirement to use the AppSettings.GetConnectionString() method was imposed on me rather late in the game. I used to use something very similar to what it generates for me. There are quite a few places that call the default constructor. I am aware that change them all to create the data context in another way (using a different constructor, static method, factory, ect..). That kind of change would only be slightly annoying since it would only have to be done once. However, I feel, that it is sidestepping the real issue. The dbml file and configuration files would still contain an incorrect, if unused, connection string which at best could confuse other developers.

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about datacontext