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:
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?
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 I dould 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.
edmx is the modeling file for Entity Framework. dbml is the modeling file for Linq 2 Sql. You should spend your time learning Entity Framework as Linq 2 Sql is deprecated.
Luckily there are some workarounds to fix this: Option 1: Remove the changed tables from the DBML designer and drag them again from the SQL explorer window. Option 2: Use the SqlMetalPlus addin. The add-in adds some extra context menu options to all the DBML files in the solution.
DBML (Database Markup Language) is an open-source DSL language designed to define and document database schemas and structures. It is designed to be simple, consistent and highly-readable.
While in the designer for the DBML, you can right-click on any white-space, and click on Properties (not the same as right-clicking on the DBML file and clicking Properties). From there, expand the "Connection" option. Set "Application Settings" to False and clear out the "Connection String" setting. These settings are what the designer uses to create that default constructor.
From there, you can use the default constructor you've created outside of the designer.cs file. Unfortunately, you'll have to repeat this process everytime you add any new tables to the designer. It's annoying, and I feel your pain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With