C# How to get the Linq To SQL DBML file to read the connection string from your web.config or app.config

You know the drill. You start a new project and drag and drop the DBML file to a project to connect to a SQL (or one of the supported databases).

Only to find  that the DBML file REFUSES to read changes to the connection string specified in your app.config or web.config file.

I recently figured out a clean and effective way to do this and wanted to put it out there – primarily for my own sake.

Considering you are working on a web project and have restricted your data-access layer to a DLL project – this is the worst case scenario. It is easier to have the DBML file inside of your web project and change the connection string.

Anyways, here goes. This should probably work for all scenarios.

Step 1: Add the connection string to your web.config
For example:

<connectionStrings>
 <add name="DefaultConnection" 
      connectionString="Server=.\SQLEXPRESS;Database=my-awesome-db;Trusted_Connection=True;" 
      providerName="System.Data.SqlClient" /> 
</connectionStrings>

Step 2: Create your DBML file and set it up.

In this example, I am naming the file: Db.dbml and have put in a Dll Project\Db\ folder so that we have the file at location: Project\Dll\Db\Db.dbml

01-Dbml structure

On expanding the file, you’ll get two additional files: Db.dbml.layout and Db.designer.cs

Step 3: Set the Connection String to (None) for the dbml file

If you double-click on the Db.dbml file in the solution explorer, the designer window opens up.
Right click on the white space of the designer and select Properties

You will see something like this in your Properties inspector

02-ConnectionStrings

This next step is important as it will let you create a new Empty Constructor to plug in the value from your web.config

Select the drop down next to the Connection property and select (None)

Save your changes.

03-ConnectionString-None

Step 4: Create a new partial class to plug in the connection string from the web.config

Add a new class to the Db folder. I call mine Db.cs which is the same as the dbml file. Though the name is not important.

Open up the class file and define an empty constructor like this:

Of course, depending on your project and dbml file name, the name of the constructor, namespace and class will change.

namespace Project.Db
{
 public partial class YourDbDataContext : System.Data.Linq.DataContext
 {
     public YourDbmlFilenameDataContext() :
         base(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, mappingSource)
     {
         OnCreated();
     }
 }
}

My web.config contains the connection inside the DefaultConnection property.
Please add the property name from your web config inside the base() shown above and you should be good to go.

Questions? Concerns? Better ways to do this? Please let me know in the comments below.
Thanks.

banner

How to setup Amazon Elastic Beanstalk within a VPC Configuration

Amazon Elastic Beanstalk has been a life saver in not only letting us deploy quickly to our staging and production servers – but also making it fairly easy to scale horizontally. However, if you want to do this under a VPC, things get complicated quickly and you’ll find yourself running into issues with no explanations.

I had to stumble and fall quite a bit before I was able to get this working consistently.

This post will give you step by step instructions on how to set this up quickly. If you follow along, you should have yourself your Elastic Beanstalk chugging along behind a VPC. I will explain wherever I can – but if something is confusing, please leave a comment.

Stage 1: Setting up your VPC

Step 1: Let’s fire up the VPC Console:

Let's fire up the VPC Console
Let’s fire up the VPC Console

 

Step 2: Click on the Your VPC sub menu

Click on the Your VPC sub menu
Click on the Your VPC sub menu

Step 3: Let’s create a new VPC from scratch

Let's create a new VPC from scratch
Let’s create a new VPC from scratch

Step 4: Add in a proper name tag and leave the CIDR block as: 10.0.0.0/16

Add in a proper name tag and leave the CIDR block as: 10.0.0.0/16
Add in a proper name tag and leave the CIDR block as: 10.0.0.0/16

Step 5: Then click on Subnets in the sub menu and Create a Subnet

Then click on Subnets in the sub menu and Create a Subnet
Then click on Subnets in the sub menu and Create a Subnet

Step 6: Let’s create our first subnet. Select one of the availability zones and the CIDR can be: 10.0.0.0/24
(Please note that this is a /24 and not /16 as we did for the VPC)

Let's create our first subnet. Select one of the availability zones and the CIDR can be: 10.0.0.0/24
Let’s create our first subnet. Select one of the availability zones and the CIDR can be: 10.0.0.0/24

Step 7: Repeat this step to create another subnet. As this is going to be load balanced, we should have atlease 2 subnets. Note that this CIDR block is: 10.0.1.0/24
Also, note that this is being created in an availability zone that is different from Step 6.

Repeat this step to create another subnet. As this is going to be load balanced, we should have atlease 2 subnets. Note that this CIDR block is: 10.0.1.0/24
Repeat this step to create another subnet. As this is going to be load balanced, we should have atlease 2 subnets. Note that this CIDR block is: 10.0.1.0/24

Step 7.1: If you have more availability zones, repeat the steps above to create one subnet per availability zone. So you may create a 10.0.2.0/24 in us-east-1d and a 10.0.3.0/24 in us-east-1e

Step 8: We now need to modify the Auto-Assign Public IP property.
Select each of the subnets you just created from the list page and click the shown button.

We now need to modify the Auto-Assign Public IP property
We now need to modify the Auto-Assign Public IP property

Step 9: In the pop up that is displayed, ensure that the setting is checked.

In the pop up that is displayed, ensure that the setting is checked.
In the pop up that is displayed, ensure that the setting is checked.

Step 10: We now need to create an Internet Gateway so that your infrastructure can communicate with the rest of the interwebs.
Click the “Internet Gateway” link in the sub menu and then the “Create Internet Gateway” button.

We now need to create an Internet Gateway so that your infrastructure can communicate with the rest of the interwebs
We now need to create an Internet Gateway so that your infrastructure can communicate with the rest of the interwebs

Step 11: Just add a proper name for your Internet Gateway and we are set.

Just add a proper name for your Internet Gateway and we are set.
Just add a proper name for your Internet Gateway and we are set.

Step 12: We now attach the newly created IG to your VPC

We now attach the newly created IG to your VPC
We now attach the newly created IG to your VPC

 

Step 13: Just select the VPC that you had created earlier in Step 4 and hit Attach

Just select the VPC that you had created earlier in Step 4 and hit Attach
Just select the VPC that you had created earlier in Step 4 and hit Attach

 

Step 14: We now need to configure a Route Table. Select the route table that is connected to the VPC you just created.

We now need to configure a Route Table. Select the route table that is connected to the VPC you just created.
We now need to configure a Route Table. Select the route table that is connected to the VPC you just created.

 

Step 15: Once you select the route tale, scroll down and select the 2nd tab – Routes. Then click ‘Edit’

Once you select the route tale, scroll down and select the 2nd tab - Routes. Then click 'Edit'
Once you select the route tale, scroll down and select the 2nd tab – Routes. Then click ‘Edit’

Step 16: Create a new entry for 0.0.0.0/0 and add the ID of your newly created Internet Gateway from Step 11 here. You should get a drop down.

Create a new entry for 0.0.0.0/0 and add the ID of your newly created Internet Gateway from Step 11 here. You should get a drop down.
Create a new entry for 0.0.0.0/0 and add the ID of your newly created Internet Gateway from Step 11 here. You should get a drop down.

 

Step 17: We then need to associate all your subnets with this route. So click on the next tab: Subnet Associations

We then need to associate all your subnets with this route. So click on the next tab: Subnet Associations
We then need to associate all your subnets with this route. So click on the next tab: Subnet Associations

Step 18: Select all the subnets and associate it with the Route table. Click Save. Make sure the Route Table is Main (it should be by default)

Select all the subnets and associate it with the Route table. Click Save. Make sure the Route Table is Main (it should be by default)
Select all the subnets and associate it with the Route table. Click Save. Make sure the Route Table is Main (it should be by default)

 

Step 19: Finally – the Security Groups. We need to create one for this configuration! Click on Security Groups in your sub menu and then on the Create button.

Finally - the Security Groups. We need to create one for this configuration! Click on Security Groups in your sub menu and then on the Create button.
Finally – the Security Groups. We need to create one for this configuration! Click on Security Groups in your sub menu and then on the Create button.

 

Step 20: Fill in sensible names and descriptions and make sure you have selected the newly created VPC from Step 04

Fill in sensible names and descriptions and make sure you have selected the newly created VPC from Step 04
Fill in sensible names and descriptions and make sure you have selected the newly created VPC from Step 04

Step 21: We now need to configure inbound rules. Add as many rules as you want here. The 0.0.0.0/0 signifies all traffic (internet data). You may want to change this as per your requirements.

We now need to configure inbound rules. Add as many rules as you want here. The 0.0.0.0/0 signifies all traffic (internet data). You may want to change this as per your requirements.
We now need to configure inbound rules. Add as many rules as you want here. The 0.0.0.0/0 signifies all traffic (internet data). You may want to change this as per your requirements.

Step 22: Make sure you have outbound rules configured as required. Basically the configuration allows your servers to talk (outward) to the entire internet.

Make sure you have outbound rules configured as required. Basically the configuration allows your servers to talk (outward) to the entire internet.
Make sure you have outbound rules configured as required. Basically the configuration allows your servers to talk (outward) to the entire internet.

Stage 2: Configuring your Elastic Beanstalk

Phew! Believe me when I tell you that the difficult bit is behind us. Creating an Elastic Beanstalk configuration based on our VPC is an easier task.
I am going to go ahead and create an Elastic Beanstalk configuration to host a web application which is load balanced and scales automatically. If you are building something else, please choose the respective options at those places.

There are a few important things to keep in mind while setting up the Elastic Beanstalk – but I’ll highlight them as required.

Step 0: Let’s fire up the Elastic Beanstalk Console

Let's fire up the Elastic Beanstalk Console
Let’s fire up the Elastic Beanstalk Console

Step 1: Click on that link to start the creation of a new Application

Click on that link to start the creation of a new Application
Click on that link to start the creation of a new Application

Step 2: Choose an appropriate name and description for your application

Choose an appropriate name and description for your application
Choose an appropriate name and description for your application

Step 3: This tutorial deals with web servers – so that is what I am going to select here.

This tutorial deals with web servers - so that is what I am going to select here.
This tutorial deals with web servers – so that is what I am going to select here.

Step 4: Please select a configuration as per your requirements. Just make sure that the Environment Type is: Load Balancing, Auto Scaled

Please select a configuration as per your requirements. Just make sure that the Environment Type is: Load Balancing, Auto Scaled
Please select a configuration as per your requirements. Just make sure that the Environment Type is: Load Balancing, Auto Scaled

Step 5: To keep things simple, I am sticking to the Sample Application – but you can choose to upload your application if you know what you’re doing

To keep things simple, I am sticking to the Sample Application - but you can choose to upload your application if you know what you're doing
To keep things simple, I am sticking to the Sample Application – but you can choose to upload your application if you know what you’re doing

 

Step 6: Please fill this out as you see fit.

Please fill this out as you see fit.
Please fill this out as you see fit.

Step 7: This one is very important! Make sure that checkbox is enabled. Else the deployment will fail without warning.

This one is very important! Make sure that checkbox is enabled. Else the deployment will fail.
This one is very important! Make sure that checkbox is enabled. Else the deployment will fail.

Step 8: This is your normal Elastic Beanstalk Configuration page. Please make sure you fill in appropriate values at places pointed by the arrows. It is recommended to have the Multi-AZ checkbox set.

This is your normal Elastic Beanstalk Configuration page. Please make sure you fill in appropriate values at places pointed by the arrows. It is recommended to have the Multi-AZ checkbox set.
This is your normal Elastic Beanstalk Configuration page. Please make sure you fill in appropriate values at places pointed by the arrows. It is recommended to have the Multi-AZ checkbox set.

Step 9: If you are not seeing this page, then you forgot something in Step 7. However, if you do see this page, make sure all the information is correctly filled up as shown in the image.

If you are not seeing this page, then you forgot something in Step 7. However, if you do see this page, make sure all the information is correctly filled up as shown in the image.
If you are not seeing this page, then you forgot something in Step 7. However, if you do see this page, make sure all the information is correctly filled up as shown in the image.

Step 10: Finally, we are done! Hit the Launch button to start up your application behind a VPC

Finally, we are done! Hit the Launch button to start up your application behind a VPC
Finally, we are done! Hit the Launch button to start up your application behind a VPC