Entity Framework Code First with AWS SQL RDS

In this article I’m going to take you through the steps for using Amazon RDS with Entity Framework code first. I will not be covering any details related to Entity Framework. First you need to install AWS Tools for .NET. Then you will need to open up the AWS explorer from visual Studio View menu. Remember you need to have your account setup with Amazon Web Services. From this you can manipulate your AWS account.

image1SS

So first I am going to create a database using the AWS RDS. Here I will be selecting the Amazon RDS (Relational Database Services).

image2SS

Right click on the DB Instance under Amazon RDS and select Launch DB Instance.

image3SS

Select the DB instance you want. Here I will be selecting the MS SQL Server Express Edition.

image4SS

Select next. On this screen provide the instance information.

image5SS

Click next. On the next screen you can provide additional information such as port number, availability zone, etc. Make sure that you check on “Add current CIDR….” Check box to allow the aws firewall access so that your local connections will go through.

image6SS

I will be leaving the default settings on this page as this is just a demo. You can change the settings based on your requirements.

image7SS

You can view the instance details before starting on the next screen.

image8SS

Click Launch. This will take few minutes to launch the instance.

image9SS

I have already created a sample MVC 4 application which uses Entity Framework code first. As you can see the solution is layered into two sections. The data model and the web. On the web layer I have named the context as “SystemDBContext”.

image10SS

Also I have created some sample data as well. So during the database creation the database will have sample data. Also you can use this to have the default data loaded as well.

image11SS

Now the database is ready and ready to go.

image12SS

Next you need to set the connection string as given bellow on your web.config file.

image13SS

Now you are ready to roll out. Open the Package Manager console and type the given command.

image14SS

As you can see when we use ‘-Verbose’ it will show the execution summary on the console.

image15SS

At the end it will execute the seed method to populate the database with sample data or default data. So every time you drop and create databases you will always have some data available for testing.

image16SS

Now you can open up the SQL Management Studio and check everything is created successfully.

image17SS

Finally execute the application and test.

image18SS

There you have, few simple steps to connect your code first approach withSQL RDS.

Advertisements

Leave a Reply

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