How To Deploy & Publish a .NET MVC Application

This is the second post in the series called .NET MVC From a PHP Developer’s Perspective where I discuss the pros and (mostly) cons of jumping onto the .NET bandwagon. Keep in mind that I’m a php/unix guy. I’m now at week 5 and I recently deployed my .NET MVC application to a web hosting provider. This was done for a testing and debugging purpose, since I had to become familiar with the whole process.

We begin with picking the right hosting plan for your .NET MVC application, then proceed to deploying your application and the contents (images, javascript, etc). Finally I show you a little trick on how to deploy your database in a format (.bak) most hosting providers will (hopefully) accept.

Picking the Right Hosting Plan

I picked Parking.ru – a Russian Windows hosting provider, which were generous enough to provide me with a two weeks trial period. My choice was based on a few calls to several different hosting providers, their technical teams had no clue about if my .NET Framework 4, MVC 2.0 application with an SQL Express database would run on their hosting plans. The answers were all similar to:

Well we’re not sure, you may try this hosting plan, but we’re not sure. And you still have to pay in order to use it. It’s running Windows Server, but we don’t know what MVC is, so we can’t guarantee you anything, sorry.

Heh, there was always a problem with Russian hosting providers, but overall the service is getting better. Tech teams now at least know whether they’re offering Apache or nginx ;) But anyways, I was quite happy to get a free trial at Parking.ru, and since they had no FAQs on how to publish my MVC application, I ran off to Google.

Publishing Your Application and Contents

It’s not really a big deal after you get it the first time. Visual Studio can automatically publish your application and the contents to a remote FTP server, which in my case worked like a charm. The only prob I had is with the external contents such as images, stylesheets and some javascript, which all sumed up to around 500 files. I didn’t add them as resources to my project, thus they were never published. Some common sense was required here, so I fired up my favorite FTP client and flushed them all manually.

Publishing Your SQL Express Database

The major issue was of course with the database. As I wrote in my previous article, the MVC Music Store is the tutorial my application was based on. The tutorial creates the models from a database scheme, and all the data is stored in an SQL Express file with the MDF extension. My hosting provider supported backups (.bak) which I could upload to their servers, so I googled around for ways to generate such backups.

Most articles I came across mention SQL Server Management Studio, so I installed that, and after a few trial and error, I managed to get my SQL Express database inside the Studio. Now what’s weird is that the backup option didn’t work, yielding something about incorrect filenames. Of course my database was called [C:UserskovsheninDocumentsVisual Studio 2010…], but SQL queries worked quite okay, so did an SQL export which generated an SQL dump of all my data. Unfortunately that was not good enough for my hosting provider.

With a few more experimenting and searching, I came across the following query, which could be issued within Visual Studio itself (does not require the SQL Server Management Studio). It generates a .bak file which is exactly what I was looking for:

BACKUP DATABASE [C:PATHTOYOURDATABASE.MDF] TO DISK = N'C:my-database.bak' WITH NOFORMAT, NOINIT,  NAME = N'My Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10

Honestly, I have no idea what some of the options mean, but I’m sure Google can help. One thing you should pay attention to is the square brackets in the database name, especially if it contains the space character (which it will, if you saved your database in the Visual Studio directory by default). The command could not be parsed by Visual Studio, but the execution generates a .bak file on your hard drive. I instantly uploaded the file to my hosting provider, and voila!

I spent 3-4 hours in total on deploying my MVC application, but now that I got it right, takes me a couple of minutes. Then again, I really miss mysqldump, subversion and the rest. They seem to be a little more lightweight than .NET and IIS.

How to Generate Quality Data for MySQL

We all had fun with the World Database, Sakila and the others when learning MySQL (see Example Databases), but it sometimes isn’t enough to run certain experiments, benchmarks within your own schema. Of course you could write a script that would generate junk data based on your column types and populate your database with a few thousand entries, but as it turns out, Benjamin Keen already did.

Meet Generate Data – a free and open source script written in php, generates quality data for your databases. Works with MySQL and, well, pretty much with any SQL compliant database I guess. What I liked about Generate Data is that you get to pick your columns, their types, and the sample data like names, last names, integer between two values, lorem ipsum (my favorite) and a bunch of others. This is why I said “quality data”.

There are a few issues I encountered, like trying to get 5000 rows gave me only 200, but such issues could easily be solved by downloading the source code and launching it locally with a few fixing (I wonder why Benjamin did such a bad job at documenting the whole thing). It took me a few minutes to fill up 30,000 rows of sample data, so who needs the World database anyway?

The script is being updated from time to time and new features are being added, not too fast, but they are. Let’s see where Benjamin takes this by the end of this year ;)

5 Really Useful Trac Reports

For those of you who are not familiar with the project management software called The Trac Project please proceed to the Trac Guide. I’ve been working with this project management tool for quite some time now and the latest upgrades are just awesome. I believe the solution for private projects’ RSS feeds has been found a few months ago somewhere here. The iCalendar issue remains, but it’s probably solved the exact same way – http authentication, although not all iCalendar clients and RSS aggregators support that yet. My favourite Feedly doesn’t.

Before showing off the snipets that I wrote for Trac Reports I’d like to give you one little hint if you’re running Trac on an old (but stable) OS version, such as Fedora Core 8. Amazon EC2 still ships the Fedora 8 operating system as default for Linux-based EC2 instances, and a simple yum install trac will get you version 0.10.x, which is outdated. The trick here is to install:

yum install python-setuptools
yum install python-setuptools-devel

Which include a package called easy_install. Since you’ve got an old version of Trac installed, use easy_install to update it, like this:

easy_install -U trac

Bam! Wasn’t that easy? One more situation where you can use easy_install is to install Trac plugins directly from their repositories (instead of the old way by uploading source files).

Okay, now back to the reports. I made these with a little experimenting with the Trac database. It’s structure is pretty much transparent and obvious, so if you’re an SQL genius then go ahead and write your own. Here’s my most useful list …

Continue reading

Customize Posts Order in WordPress via Custom Fields

I came across this last night, might be helpful for some of you ;) Sorting posts in a customized order is pretty simple with the WordPress custom fields mechanism and custom SQL queries. Start off with a basic query:

$querystr = "
	SELECT wposts.*
	FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
	WHERE wposts.ID = wpostmeta.post_id
	AND wpostmeta.meta_key = 'order'
	AND wposts.post_type = 'post'
	AND wposts.post_status = 'publish'
	ORDER BY wpostmeta.meta_value ASC
";

That will sort the posts in ascending order using the custom field called order. This is how you actually process the query and get into The Loop:

$pageposts = $wpdb->get_results($querystr, OBJECT);

if ($pageposts)
{
	foreach ($pageposts as $post)
	{
		setup_postdata($post);
		// you can use template tags from now on
	}
}

One more thing. How to use more than one custom field in your query? Well, yes, this is SQL, not WordPress, but anyways. Let’s say you want to select all the posts marked complete (custom field) and sorted by order (another custom field). Here you go:

SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->postmeta wpostmeta2
WHERE wposts.ID = wpostmeta.post_id
AND wposts.ID = wpostmeta2.post_id
AND wpostmeta.meta_key = 'order'
AND wpostmeta2.meta_key = 'complete'
AND wpostmeta2.meta_value = '1'
AND wposts.post_type = 'post'
AND wposts.post_status = 'publish'
ORDER BY wpostmeta.meta_value ASC

Assuming the complete custom field takes a 1 when the post is complete. I used this method in a simple task manager theme I did for WordPress. Did I overuse the word “custom”? ;)