Tuesday, May 26, 2015

Azure Websites now Web Apps

I have finally been able to get into using Windows Azure more and I'm loving it. I had been using it for the last two years on and off, but with the recent updates announced at Build, it's been amazing.

I found some great articles from Rick Anderson at Microsoft about Identity, MVC5, Azure, and Identity (including OAuth2.0) The series found here gets you to a production worthy site in just a few hours that includes user registration, SMS, external logins, and all that base framework that just about any site needs.

So, here's the scenario...

I started a project in MVC4 and would like to migrate to MVC5 and use the ASP.NET Identity stuff with Entity Framework and Azure SQL.

I haven't found an article describing how to use an existing database with an existing project that did not originally use that data base. For example, the MVC4 site used abc.sqlserver.qualifiedname. After updating, I'd like to use def.sqlserver.qualifiedname on a BLANK database.

Using EF migrations enables this scenario.

From the package manager, use the following commands:

PM> enable-migrations
PM> add-migration Initial

Update your connection string for the connection which points to the users db (eg, DefaultConnection) from abc (original) to def (new azure sql) server and set initial catalog to the blank database.

PM> update-database

the first command adds the required components to enable migrations including adding a Migrations folder to you project.

second command creates a migration representing the current DB schema

third command actually sends commands to the azure sql server that you updated in the web config above to be like the original database.

Migrations are a great way of helping to enable continuous deployment of databases so I'm hoping EF turns out to be as great as everyone says :)