Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 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.

like image 330
drs9222 Avatar asked Apr 19 '10 12:04

drs9222


People also ask

What is the difference between Dbml and EDMX?

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.

How do I refresh a Dbml file?

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.

What is the code Dbml?

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.


1 Answers

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.

like image 68
drovani Avatar answered Sep 29 '22 20:09

drovani