Showing posts with label SQL2012. Show all posts
Showing posts with label SQL2012. Show all posts

Tuesday, September 18, 2012

Migrating an existing Entity Framework code first MVC4 website to Windows Azure – Part 2

My last post involved creating a simple fully functioning MVC 4 application, talking to a SQL server 2012 backend DB. In this post, my aim is to demonstrate how you could easily migrate this application and host it entirely in the cloud, using Windows Azure. In order to proceed, it is vital that you sign up for the service and have got all the right tools required to allow deploying applications and services to the cloud. Fee free to browse to the following links and familiarize yourself with the content. Trust me, its all very cool!

Go ahead and sign up for an account (if you don't already have one) and install the azure development tools for visual studio 2012. Make sure that you also sign up for the Azure Websites preview as well.

Create an Azure Website & Database instance

I begin by signing into my windows azure management portal. My first step is to create a new Azure SQL database. The database needs to be hosted via Azure, hence it is required when creating a new DB to instruct Azure to create a new server or re-use an existing server that may have been created in the past. We will simply instruct it to create a new DB server. Follow the wizard through to provide a login which can be used to access the server and complete the DB creation process

image  image

Once this is created, we must configure the new DB server to accept requests from the client machine we are currently working on. To do this, navigate to the SQL Databases tab from the navigation menu, find out newly created ToDoListDB DB instance from the list. Under the server column, we find a link to the current server created and configured by windows Azure to host our DB instance (yes its in the portion that I have had to black out as the web no longer relies on an honour system for safety these days). Clicking on it takes us to the landing page for configuring the azure server instance. Once in there, click the option to Configure the server

image

On the configuration page, the site outlines what the current client IP address is (i.e. our dev machine used to create the website previously). Please note that for safety reasons, I have blocked out some values on the page. Copy this IP address and create a new rule Allow and paste the IP address in the textbox for the start & end IP address. This configures our Azure server to accept requests from our client machine

image

Once this is done, we can go back to the Azure DB instance. Selecting the the DB instance from the list takes us to a separate page which allows us to obtain a copy of the ADO.net connection string for this DB. Make sure you maintain this connection string as we will require this later on in the post. Ensure that you have provided the correct username and password details in the connection string (not included by default).

image

Next we go ahead and create a new Azure Web site instance. The wizard prompts us to pick a URL name. Note that url names are reserved and you have pick one that is not already in use. For this blog, I have used the name toDoListBlog, may sound a bit clunky but as long as it gets the job done. Another thing to note is that currently (at the time of writing this post), it seems that all azure websites have to be affiliated with a region in the US. I am not sure if this is only a azure website preview feature but I honestly do not have any more information at present. Bear in mind that this may affect stuff like regional settings, time zones, privacy agreements etc.

image image

At this point we are all setup to migrate and host our application in the cloud using windows azure. One final step before we move on is to get the publishing profile for this website so that we can instruct VS2012 where this needs to be uploaded. Navigate to the dashboard of our newly created site, click on the option to Download Publishing Profile and save this file somewhere on your machine

image

Altering the MyToDoApp VS2012 Solution

I can now fire up my solution created in my previous blog post. Since I have all the azure SDK & tools for VS2012 installed, I can go ahead and add a new Windows Azure cloud service project to my solution. Once you have instructed VS2012 to create the solution, a small wizard pops up prompting us to add new .Net roles to the cloud service solution. In our case, we do not need to add any new roles, so we can simply click Ok.

image   image     

Once your solution is setup, navigate to the newly created MyToDoApp.CloudSvc project and find the item marked Roles. Right click and from the options, choose Add Web Role Project from this Solution. Select the MVC4 project MyToDoApp from the wizard and click ok

image    image

This gets VS2012 to modify your current solution and adds a number of references to the project to allow it to be hosted inside the cloud. We can then add a reference to our previously created azure DB, ToDoListDB. Using the copied connection string, I can add an entry to my release configuration file (i.e. Web.Release.config) like so:

<?xml version="1.0"?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <connectionStrings>

<add name="ToDoListDb"
providerName="System.Data.SqlClient"
connectionString="Server=tcp:[Server Name].database.windows.net,1433;Database=ToDoListDb;User ID=[User Name]@[Server Name];Password=[Password];Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
xdt:Transform="SetAttributes"
xdt:Locator="Match(name)"/>

</connectionStrings>

<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>



This ensures that during release deployment, our application will connect to and use the Azure SQL DB as its persistence store. The original connection string added in my previous post still resides in the parent Web.Config. This allows us to utilize the local SQL 2012 DB on our dev machines during debugging.



Publishing Application to Windows Azure


We are now finally ready to deploy our application to WIndows Azure. In the solution explorer, right click on the MyToDoApp project and select Publish. In the corresponding Publish Wizard interface, within the Profile section, find and import the azure *.publishsettings file previously saved. This should automatically populate the next section of the interface, Connection. Feel free to validate your connection with the cloud service. This ensures that your client machine’s IP is capable of deploying to Azure


image  image


Moving onto the next section, Settings, set the configuration as Release. In the sections labelled Databases, ensure that your connection string to the Azure DB is set. Ensure the following options shown below are also ticked. These will ensure that the code migrations are deployed and run on the target DB


image


And were done! Feel free to preview the deployment. Hit the publish button. The first upload usually takes a while, but any subsequent uploads are capable of merging changes with the existing files. No, I have still not worked out when I am going to mow my lawn.


image

Thursday, September 13, 2012

Migrating an existing Entity Framework code first MVC4 website to Windows Azure – Part 1

I have been playing with some awesome new toys from Microsoft this week, namely Entity Framework Code First and Windows Azure. As I was working through all the awesome project templates that create & add web roles etc., one thing that struck me as a common usage scenario for anybody that would like to migrate to the cloud, would basically involve migrating an existing site into Azure and hosting it from an Azure website. I thought I might try working out how to do this. Since this may turn into a mega post,I thought I might break it up into a couple of posts.

As part of this post I will first create a simple MVC 4 standalone website from scratch. To add a bit of extra jazz, I am going to build it using Entity Framework Code First, using a SQL 2012 backend. I will then migrate and host both the site and database in Azure.

To start off, let us describe a very simple idea for a website. Assume we wish to create a simple site that will allow us to create and maintain a TODO list of items, basically something we can use to create, timestamp and manage things to do throughout our day. I must stress, its going to be a very simple solution for demonstration purposes. To start off with, within VS2010, create a simple MVC 4 solution, labelled ‘MyToDoApp’, using the Basic project template.
image   image

Creating the Entity Model

Once the solution is created, note that the project template should have already enabled Entity Framework for the solution. Verify this via the package manager console via the following command
image
The next step, I am going to go ahead and create my entity model. To keep things simple, we only need 1 entity in our model, a Task entity to capture all the information we need. We create the entity class like so (note the namespace):
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:   
   6:  namespace MyToDoApp.Models
   7:  {
   8:      public class Task
   9:      {
  10:          public int Id { get; set; }
  11:   
  12:          public string Description { get; set; }
  13:          
  14:          public DateTime? DueBy { get; set; }
  15:          
  16:          public bool Completed { get; set; }
  17:      }
  18:  }



I purposely chose the class name ‘ToDoTask’ over the simple and obvious ‘Task’ mainly to avoid any conflicts with the .NET TPL library. Also I have left made the DueBy property nullable as we may have a task that does not need to be completed by some time framed (like mowing the lawn). My next step is to create a an EF context. We can do this like so:


   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Data.Entity;
   4:  using System.Linq;
   5:  using System.Web;
   6:   
   7:  namespace MyToDoApp.Models
   8:  {
   9:      public class TaskListContext: DbContext
  10:      {
  11:          public DbSet<ToDoTask> Tasks { get; set; }
  12:      }
  13:  }





This is all the code we need to define out entity model (yes it is that awesome).

Using Entity Framework Code-First to push the entity schema to an external database


To mimic a real world scenario, I want Entity Framework to translate and host the entity model on my local SQL 2012 DB. I proceed by creating a simple database which we can label ‘ToDoListDb’. Once this is done, we need to instruct entity framework where it needs to point and create the DB schema to. Navigate to the Web.config in your solution, find the element labelled ‘connectionStrings’ & add a new connection entry, similarly labelled ‘ToDoListDb’, to our newly created DB as shown. You may comment out/delete the existing connection string entry labelled “DefaultConnection” (created by default by the project template)
<connectionStrings>
    <add name="ToDoListDb"
         providerName="System.Data.SqlClient"
         connectionString="Data Source=.;Initial Catalog=ToDoListDb;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False" />
    
  </connectionStrings>



We may now proceed to translate our entity model to the database. Navigate to the Nuget Package manager console and run the following commands (in the same sequence). First we will instruct EF to enable code first migrations in our project, telling it that we would like to use the DB in the connection string we added previously to the web.config (Note: make sure you specify this parameter using double quotes)

PM> Enable-Migrations -ConnectionStringName "ToDoListDb"



Next we instruct EF code first to add a base migration to our project to reflect the current state of the entity model, again passing in the connection string as a parameter

PM> Add-Migration Base -ConnectionStringName "ToDoListDb"



Finally we ask it to update the database

PM> Update-Database -ConnectionStringName "ToDoListDb"



If all goes as planned, we should see a schema applied to our database like so

image

Creating the MVC Web UX

Now that we have our entity model created and deployed to our SQL db, we now need  to instruct the TaskListContext initially created to connect to and utilize the ‘ToDoListDb’ created. We can do this via a constructor for the TaskListContext class, which can accept a connection string as a parameter and pass this onto its superclass DbContext like so:

   1:  namespace MyToDoApp.Models
   2:  {
   3:      public class TaskListContext: DbContext
   4:      {
   5:          public DbSet<ToDoTask> Tasks { get; set; }
   6:   
   7:          public TaskListContext(string connectionString)
   8:              :base(connectionString)
   9:          { }
  10:      }
  11:  }

  To simplify creating an instance of the TaskListContext class, we may use a Factory to create and serve new instances to any MVC controller that wishes to utilize it:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Configuration;
   6:   
   7:  namespace MyToDoApp.Models
   8:  {
   9:      public static class TaskListContextFactory
  10:      {
  11:          public static TaskListContext GetNewContext()
  12:          {
  13:              //get the connection string entry "ToDoListDb"
  14:              var connectionString = ConfigurationManager.ConnectionStrings["ToDoListDb"].ConnectionString;
  15:              return new TaskListContext(connectionString);
  16:          }
  17:      }
  18:  }

The TaskListContextFactory class simply uses the System.Configuration.ConfigurationManager class to retrieve the connection string from our web.config and pass that as an argument to our TaskListContext, ensuring any instance of the context is always communicating with the correct DB. Being a static class adds to the convenience of just being able to invoke any of its methods statically to get a new context. Our next step is to create an MVC controller & view to display all the tasks. The view will also allow the user to add a new task, edit & remove existing tasks. We start with specifying the controller TaskController like so:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Web.Mvc;
   6:  using MyToDoApp.Models;
   7:   
   8:  namespace MyToDoApp.Controllers
   9:  {
  10:      public class TaskController : Controller
  11:      {
  12:          TaskListContext taskContext;
  13:   
  14:          public TaskController()
  15:          {
  16:              taskContext = TaskListContextFactory.GetNewContext();
  17:          }
  18:   
  19:   
  20:          //display all tasks
  21:          public ActionResult Index()
  22:          {
  23:              return View(taskContext.Tasks);
  24:          }
  25:   
  26:      }
  27:  }

The Index mvc action method simply selects all the tasks and passes them as a parameter to the view. Next we create the strongly typed Index.cshtml MVC View to simply list all the tasks:
@model IEnumerable<MyToDoApp.Models.ToDoTask>

@{
    ViewBag.Title = "My Task List";
}

<h2>My Task List</h2>

<table>
    <thead>
        <tr>
            <th>Description</th>
            <th>DueBy</th>
            <th>Completed</th>
        </tr>
    </thead>
    <tbody>

        @foreach (var task in Model)
        {
            <tr>
                <td>@Html.DisplayFor(model => task.Description)</td>
                <td>@Html.DisplayFor(model => task.DueBy)</td>
                <td>@Html.DisplayFor(model => task.Completed)</td>
            </tr>    
        }

    </tbody>
</table>

Ensure you also have the following rout configured for the application in your RouteConfig.cs

   1:   public static void RegisterRoutes(RouteCollection routes)
   2:          {
   3:              routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
   4:   
   5:              routes.MapRoute(
   6:                  name: "Default",
   7:                  url: "{controller}/{action}/{id}",
   8:                  defaults: new { controller = "Task", action = "Index", id = UrlParameter.Optional }
   9:              );
  10:          }



If we create and run the application we now have the *currently empty’ list of tasks

image

Next we want to be able to allow adding a new ToDoTask. We start by providing an Action-link in our current MVC view like so:
@model IEnumerable<MyToDoApp.Models.ToDoTask>

@{
    ViewBag.Title = "My Task List";
}

<h2>My Task List</h2>

@Html.ActionLink("Add Task", "add")

<table>
    <thead>
        <tr>
            <th>Description</th>
            <th>DueBy</th>
            <th>Completed</th>
        </tr>
    </thead>
    <tbody>

        @foreach (var task in Model)
        {
            <tr>
                <td>@Html.DisplayFor(model => task.Description)</td>
                <td>@Html.DisplayFor(model => task.DueBy)</td>
                <td>@Html.DisplayFor(model => task.Completed)</td>
            </tr>    
        }

    </tbody>
</table>

This is followed by adding the Add action method to our TaskController class. Notice that I am explicitly stating the name of the MVC view to use, reason being I intend to reuse the same view for the edit template. Also, as per best practise in MVC, I pass a new ToDoTask instance to the view, defaulting the Completed flag to false:


   1:  public ActionResult Add()
   2:          {
   3:              return View("TaskEdit", new ToDoTask() { Completed=false});
   4:          }

The TaskEdit.cshtml view contains the following html
@model MyToDoApp.Models.ToDoTask

@{
    ViewBag.Title = "Edit Task";
}

<script type="text/javascript">
    //use JQuery to add a datepicker
    $(function () {
        $("#@ViewData.TemplateInfo.GetFullHtmlFieldName("DueBy")")
            .datepicker({ dateFormat: 'dd/mm/yy' });
    });
</script>

<h2>Edit Task</h2>
@using (Html.BeginForm())
{
    @Html.EditorForModel()
    <input type="submit" name="submit" value="Save" />
}

Debugging the site in its current state should render the following

image   image


This outlines a tiny issue, the Id member of a ToDoTask should not be editable as it is the primary and hence automatically incremented using an Identity in TSQL. We can fix this by editing the class ToDoTask to add a HiddenInput data annotation attribute to the Id member field like so (line 7-8):


   1:  using System.Web.Mvc;
   2:   
   3:  namespace MyToDoApp.Models
   4:  {
   5:      public class ToDoTask
   6:      {
   7:          [HiddenInput(DisplayValue=false)]
   8:          public int Id { get; set; }
   9:   
  10:          public string Description { get; set; }
  11:          
  12:          public DateTime? DueBy { get; set; }
  13:          
  14:          public bool Completed { get; set; }
  15:      }
  16:  }
This ensures that MVC’s scaffolding renders the field as a Hidden input when editing a ToDoTask model item

image

Finally, we handle the POST back when the user clicks save to persist the task. We add the following MVC action method to the TaskController.cs:

   1:  [HttpPost]
   2:          public ActionResult Add(ToDoTask task)
   3:          {
   4:              if(ModelState.IsValid 
   5:                  && task !=null)
   6:              {
   7:                  taskContext.Tasks.Add(task);
   8:                  taskContext.SaveChanges();
   9:                  return RedirectToAction("Index");
  10:              }
  11:              return View();
  12:          }
Once this is done, we can easily create new tasks for the list

image    image

The final step is to add some actions to allow editing and deleting values from the list. For the sake of brevity I will now simply list the additional actions I will add to support deleting and editing items in TaskController.cs:

   1:   public ActionResult Edit(int? Id)
   2:          {
   3:              if(Id == null)
   4:                  throw new ArgumentNullException("Id");
   5:   
   6:              if (taskContext.Tasks.Any(tsk => tsk.Id == Id))
   7:              {
   8:                  var task = taskContext.Tasks.FirstOrDefault(tsk => tsk.Id == Id);
   9:                  //we are reusing the TaskEdit.cshtml view that was added earlier
  10:                  return View("TaskEdit",task);
  11:              }
  12:   
  13:              return RedirectToAction("Index");
  14:          }
  15:   
  16:          [HttpPost]
  17:          public ActionResult Edit(ToDoTask task)
  18:          {
  19:              if (task != null)
  20:              {
  21:                  taskContext.Tasks.Attach(task);
  22:                  //signal that the entity has been modified
  23:                  taskContext.Entry(task).State = System.Data.EntityState.Modified;
  24:                  taskContext.SaveChanges();
  25:              }
  26:              return RedirectToAction("Index");
  27:          }
  28:   
  29:          public ActionResult Delete(int? Id)
  30:          {
  31:               if(Id == null)
  32:                  throw new ArgumentNullException("Id");
  33:   
  34:              if (taskContext.Tasks.Any(tsk => tsk.Id == Id))
  35:              {
  36:                  var task = taskContext.Tasks.FirstOrDefault(tsk => tsk.Id == Id);
  37:                  taskContext.Tasks.Remove(task);
  38:                  taskContext.SaveChanges();
  39:              }
  40:   
  41:              return RedirectToAction("Index");
  42:          }

And finally modify the Index.cshtml view to be able to invoke these extra actions:
@model IEnumerable<MyToDoApp.Models.ToDoTask>

@{
    ViewBag.Title = "My Task List";
}

<h2>My Task List</h2>

@Html.ActionLink("Add Task", "add")

<table>
    <thead>
        <tr>
            <th>Description</th>
            <th>DueBy</th>
            <th>Completed</th>
        </tr>
    </thead>
    <tbody>

        @foreach (var task in Model)
        {
            <tr>
                <td>@Html.DisplayFor(model => task.Description)</td>
                <td>@Html.DisplayFor(model => task.DueBy)</td>
                <td>@Html.DisplayFor(model => task.Completed)</td>
                <td>@Html.ActionLink("Edit", "edit", new { Id = task.Id})</td>
                <td>@Html.ActionLink("Delete", "delete", new { Id = task.Id})</td>
            </tr>    
        }

    </tbody>
</table>
This concludes our simple MVC4 application. We now have our application in place, communicating with a backend SQL 2012 database.

image  image

This mimics a common situation we may all face, whereby we have a standalone web application communicating with an existing backend SQL DB. At this point, there is no suggestion that any of this code should be hosted on Azure anywhere in the solution. In my next post, I will outline how easy it is to host and run this MVC application from Azure