Home > Developing Using CodeFluent Entities, SQLAzure > Quickly Generate SQL Azure Databases with CodeFluent Entities

Quickly Generate SQL Azure Databases with CodeFluent Entities


CodeFluent Entities ships out-of-the-box with an SQL Azure producer which allows you to generate databases to SQL Azure directly from Visual Studio, based on a CodeFluent Entities model. We’ll see in this post how we can actually do it.

First of all, we have to setup our Azure environment, so we’ll start by login on www.windowsazure.com with the LiveID associated with an Azure subscription. Once we’re logged on, we’ll go directly to the Database section of the portal and we’ll begin by creating a new database server as shown here:

Then you’ll have to choose a region to define where your server will be located. In my case it will be “North Europe”. Once you have chosen a region, you’ll have to provide credentials for the server as shown below:

The next step is very important here, as you’ll have to configure the firewall of your server to allow some IP ranges. The thing is, if you don’t allow your own IP address, you won’t be able to manage the database or generate it with the CodeFluent Entities SQL Azure producer, from the Visual Studio environment:

Once you’ve setup your firewall rule(s), click on finish to validate the creation of you server.

The server is now created, so click on it and you should see that a database has been created by default, the master database. But we’re not able to produce anything on this one, so we’ll have to create a new one. To do that, click on “Create” on the top on the database part of the ribbon, then give a name to your database and select its edition and size. In my case, I chose a 1GB Web edition which I named “MyDatabase”.

Azure Database configuration is now done so let’s open Visual Studio to see how to generate the persistence layer of our application directly on the database we’ve just created. For the purpose of this article I’ll use a sample project provided out-of-the-box with CodeFluent Entities, namely the “Advertising Sales Management Sample Model”. If you open this model, you’ll see it contains 7 entities, 4 enumerations, and different relations (e.g. one-to-one, one-to-many, many-to-many):

Once this project is created, I’ll add a new “SQL Server Database”-type project to my solution, which will be the target project for the CodeFluent Entities SQL Azure producer. I’ll name this project “SoftFluentArticle.Database” as shown here:

Once that’s done, let’s add a new producer to the CodeFluent Entities project, the ‘SQL Azure producer’. It can be found in the “Persistence Layer Producers” category as shown here:

As you can see, if you never used it, it’s pretty similar to the regular CodeFluent Entities SQL Server producer, but you will notice there are are a “Local SQL Server” and an “Online SQL Azure” sections. Moreover, in the “Targets” section, the property “Produce Online SQL Azure” is set to false and the property “Produce Local SQL Server” is set to true.

The reasons are multiple. The most important is simply… the cost for you :-) Because each time your application requests data from your SQL Azure, there is a cost applied to the transaction and you certainly don’t want to pay fees for these transactions while you’re developing/testing your application. That’s why you will use the Local SQL Server most of the time, and switch to the Online SQL Azure when you’re ready. It’s important to note the generated code will always target SQL Azure, even if the local SQL Server is in use, thus avoiding pure SQL Server statements unsupported on SQL Azure.

We now have to specify our SQL Azure connection string, just like a regular SQL Server connection string. To do that just click on the “Connection String” property line, a button will be shown at the extreme right of the line, click on it and you should get the same popup as the one shown below. Fill it with your Database Name, Server Name, Password, and User name. You can now click on test connection to check it works.

Back to our producer configuration, choose the database project we’ve created earlier as the “Default Target Directory” (you can use a sub folder if you want, or the root as it’s shown here):

Click on “Ok” to confirm all that.

Everything is now setup, so let’s build our project and see what happens! As for the SQL Server producer, the SQL Azure produced our scripts in the database project folder we’ve set as the target (see all the .SQL files automatically generated):

Now, let’s open the well-known SQL Server Management Studio tool and connect to our local SQL Server. You should see there the database which has been generated by the SQL Azure producer “diff engine”. Open now you web browser, go to your SQL Azure database address, in my case https://ttmshoc6av.database.windows.net/?langid=en#$database=MyDatabase, and fill-in the form with the server credentials you’ve setup earlier.

When you are logged on, click on the Design button on the bottom left of the screen. You should land on the following screen and, as you can see, there are no tables or stored procedures here, yet:

Why hasn’t my database been generated on SQL Azure? The answer is simple: the property “Produce Online SQL Azure” is still set to false. So let’s go back to Visual Studio. Double-click on the “SQL Azure Producer”, change that property to true, press OK, and build your solution again. Once your solution is built, go back to the SQL Azure portal, click on the Refresh button. All your tables and stored procedures should now appear like this:

Since you’re using CodeFluent Entities, as usual, continuous build is a feature. So, you can change the model and build anytime, this will update all the layers of your application, including your SQL Azure database.

Pretty cool isn’t it?

Aymeric ROLAND.

  1. April 29, 2013 at 6:01 am | #1

    Another benefit of this product is its cost.

  1. No trackbacks yet.

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

Follow

Get every new post delivered to your Inbox.

Join 41 other followers