Quick Tip: Grab your ConnectionStrings From the config file directly

Interesting little tidbit discovered today. I was debugging a helper method that took a connection string as an input parameter, so that it could create a simple SQL connection (no entity framework magic) in order to process things. Since there was some EF code and context already established, the original code was setup to pass the connection string from the existing context:

context.Database.Connection.ConnectionString

However, I was getting an error when connecting. A little digging brought to light the fact that when you pull the connection string in this manner, the password is omitted. I suspect for security purposes. That said, simply using the standard:

ConfigurationManager.ConnectionStrings["DefaultContext"].ConnectionString

…will get you what you need.

quick-tips.jpg

What are your 10 bits on the matter? I want to know!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s