Story Details for tools

MvcInstaller - NuGet Package for MVC 3 applications

kahanu
Author:
Version:
2.0.0.0
Views:
508
Date Posted:
8/27/2011 5:51:02 PM
Date Updated:
8/27/2011 5:51:02 PM
Rating:
5/1 votes
Framework:
ASP.NET MVC
Platform:
Windows
Programming Language:
C#
Technologies:
NuGet, MvcInstaller
Tags:
nuget, mvcinstaller, installer, asp.net mvc 3, securityguard
Demo site:
Home Page:
https://github.com/kahanu/MvcInstaller
Share:
MvcInstaller is a NuGet package that allows site administrators to easily install their database schema and/or ASP.NET Membership system for their ASP.NET MVC application on their production server through a browser.

There are a few pre-requisites to know about before deciding whether to use it.
  1. ASP.NET MVC - MvcInstaller is built on the ASP.NET MVC framework so it can only be used to install MVC applications, not WinForms, or WebForms, etc.
  2. SQL Server - at this moment, it only installs your database schema on the SQL Server RDBMS.
  3. Database - you need to have the database created PRIOR to running MvcInstaller.  MvcInstaller works very easily with MVC sites that are installed on shared hosting systems. Your application's database tables do NOT need to be created prior to installing or running MvcInstaller.  They can be done with this process. Read on.

Procedures to use MvcInstaller

There are a few steps that are taken to use MvcInstaller.  These are not rigid steps, but general guidelines.

Step 1

Create your ASP.NET MVC application.  The first step is to obviously create your ASP.NET MVC application.  When you have completed the application, install MvcInstaller to prepare for deployment.  NOTE: MvcInstaller can be installed into your application via NuGet at any point in your development cycle.  It does not have to be installed when you first start your project. 

Step 2

Configure MvcInstaller.  There are some minor settings to configure in the MvcInstaller installer.config file.  These settings are what MvcInstaller will execute to install your database schema and the ASP.NET Membership with Roles and Users.

Step 3

Publish ASP.NET MVC application.  Publish (FTP) your compiled ASP.NET MVC application to you production server.

Step 4

Install your database schema and ASP.NET Membership system using MvcInstaller via a browser.

I'll go over everything that is necessary for this in the information below.

Installation

Installation of the MvcInstaller into your MVC application couldn't be easier.  You have to different methods to use:

Visual Studio Reference Dialog - you would right-click on the References folder of your application and select "Manage NuGet Packages".

Manage NuGet Packages menu item

Once you click this menu item you will see the dialog to choose the NuGet packages.

package manager dialog

In the Search box in the upper-right corner of the dialog, enter the name of the package you want, or something like it.  In this case I've entered: "mvcinstaller" and it shows me my three NuGet packages.

NOTE: the third package "MvcInstaller", should NOT be chosen because I don't maintain it any longer.  I do maintain the other two, "MvcInstaller.MVC2" and "MvcInstaller.MVC3".  If you hover your mouse over a package, you'll see an "Install" button appear.  Click the button to install the package.

Package Manager Console - this is the way to install and manage packages via a command-line like process.  You can display the console by clicking the Tools menu and then choosing "Library Package Manager", and then "Package Manager Console".  The console will appear at the bottom of the Visual Studio instance.

package manager console

To install the package simply type: install-package MvcInstaller.MVC3

If you enter, install-package MvcInstaller and then click the Tab key, you'll get the Intellisense for the available versions as shown above.

Once you hit the Enter key, it will install the package.

NOTE: either one of these methods are fine for installing MvcInstaller, but some NuGet packages require that you use the console because the NuGet package needs to run PowerShell scripts, and they can't be executed via the dialog.

Configuration

After MvcInstaller has been installed you'll see a number of different files that have been included in the MVC application.  First is the MvcInstaller assembly.

assembly reference

Next are some files that have been added to the project.

files added to project

In order to prepare the application to use MvcInstaller we need to configure the installer.config file.  Here's a look at the installer.config file.

01.<?xml version="1.0" encoding="utf-8" ?>
02.<InstallerConfig>
03.  <ApplicationName>MyCoolApplication</ApplicationName>
04.  <Path>
05.    <RelativeSqlPath>App_Data</RelativeSqlPath>
06.  </Path>
07.  <Membership Create="true" ProviderName="AspNetSqlMembershipProvider"/>
08.  <Profile ProviderName="AspNetSqlProfileProvider" />
09.  <RoleManager ProviderName="AspNetSqlRoleProvider">
10.    <Roles>
11.      <Role Name="Administrator">
12.        <Users>
13.          <User UserName="admin" Password="password" Email="me@info.com" SecretQuestion="Favorite Color" SecretAnswer="Mauve" />
14.          <User UserName="bizuser" Password="93o404u" Email="ninjaburp@aol.com" SecretQuestion="Favorite Movie" SecretAnswer="Saturday Night Fever" />
15.        </Users>
16.      </Role>
17.      <Role Name="Manager">
18.        <Users>
19.          <User UserName="joemanager" Password="mypassword" Email="jmanager@myemail.com" SecretQuestion="Dog's Name" SecretAnswer="Thor" />
20.        </Users>
21.      </Role>
22.    </Roles>
23.  </RoleManager>
24.  <Database UseTrustedConnection="true" EntityFrameworkEntitiesName="MyCoolEntities">
25.    <ConnectionStringName>MySampleConnection</ConnectionStringName>
26.    <DataSource>localhost</DataSource>
27.    <InitialCatalog>MyCoolDb</InitialCatalog>
28.    <UserName>mycooldbadmin</UserName>
29.    <Password>mycooldbpassword</Password>
30.  </Database>
31.</InstallerConfig>

I'll go over each section here.
  • ApplicationName - (line 3) - this is what is used in your web.config membership sections for the ApplicationName.  It will update the web.config membership sections as well as in the database.
  • Path - (lines 4 - 6) - the App_Data name is the folder where your SQL scripts should be placed to generate the tables for your database schema on the production database.  You should be very careful and number your SQL scripts in the order that they should be executed.  MvcInstaller will add them to a sorted collection and then execute them in that order, so it's up to you to make sure they are ordered correctly.
  • Membership - (line 7) - this line does two things, it tells MvcInstaller whether or not it's going to create the ASP.NET Membership system for you, and what the provider name is.  The default ASP.NET membership provider is automatically included for you, but if you have created a custom provider, you can add it here.  NOTE: very important, the Create attribute is an all-or-nothing action, meaning if this is set to True, then all three of the providers will be created, Membership, Profiles, and Roles.  If it's False, then none will be created.
  • Profile - (line 8) - this sets the name of the Profile provider.
  • RoleManager - (lines 9 - 23) - this is where you will enter the names and roles of the administrators for your application and the Roles provider name.  NOTE: it's important to understand that MvcInstaller is primarily built for Administrators, meaning it's supposed to be used by Administrators so they can quickly get their database schema installed with the Membership system in order to have instant access to the application.  It is not necessary or advised to try and include users and roles for non-administrative access.  That should be done by Administrators after the application has been installed.

    You have the capability to configure MvcInstaller to install any number of roles and users-to-roles by including as many Role nodes as needed.  Then you just need to include the users for those roles and you are all set.
  • Database - (lines 24 - 30) - these lines cover all the settings necessary to connect to your production database. The UseTrustedConnection attribute tells the application whether the connection is trusted.  If it is then it will generally use Windows Authentication and a username and password are not needed, and the username and password config settings will be ignored.  Otherwise, if it's set to False, then the username and password values are required.

    The EntityFrameworkEntitiesName attribute tells MvcInstaller whether it will create a connection string for the Entity Framework.  If it's blank, then it will not, if the value is not blank, then it should be the EntityFramework Context name.
  • ConnectionStringName - (line 25) - this of course is the name of the connection string as it will be created for you in the web.config.  
  • DataSource - (line 26) - this is the name of the SQL Server instance that you will be connecting to.
  • InitialCatalog - (line 27) - this is the name of the database.
  • UserName and Password - (lines 28 and 29) - these are the credentials for the database if the UseTrustedConnection value is set to False.

Update - January 3, 2012

Version 2.0.0.0 - changed the way the EntityFramework connection string is built to be more reliable for different configurations.  The proper way to make this work now is the following:

  1. EntityFramework metadata - open the App.config for your EntityFramework context (if you've built it using the visual designer) and copy the metadata information
  2. Installer.config - paste this into the EntityFrameworkEntitiesName value

Example

Open the App.config file for the EntityFramework and copy the metadata name:

app.config

... and paste it in the installer.config file.

installer.config

MvcInstaller works on conventions, so this will name the EntityFramework connection string as this name and also build the connection string with this name, which we know works since it's in the App.config file.

In this instance, the ConnectionStringName value is irrelevant when the EntityFrameworkEntitiesName has a value.  Again, when the EntityFramework is being used, another connection string will be created for the ASP.NET Membership system, and this will be called "MembershipConnection".

New as of October 28, 2011

The Membership providers, Membership, Profile and RoleManager elements, all can accept a "type" attribute so you can enter your custom provider if necessary.  If you do not add a "type" attribute to any of the membership elements, then the default Microsoft providers will be used when modifying your web.config.

The install.config file will look like this with the optional "type" attribute for a custom provider.


<Membership Create="true" ProviderName="AspNetSqlMembershipProvider"/>
<Profile ProviderName="KingsCustomSqlProfileProvider" type="GB.KingsCustomSqlProfileProvider, GB" />
<RoleManager ProviderName="AspNetSqlRoleProvider">

Your Database Schema

In order to install your application's database schema, you should prepare SQL scripts for the tables, stored procedures, views and any seed data and place them into the "App_Data" folder.

Important! As I mentioned earlier, you should number these sql scripts in the order you want them to execute.  Something like this...

1 - Tables.sql
2 - Data.sql
3 - StoreProcedures.sql

The installer will grab all sql scripts in the "App_Data" folder and install them in their numbered order.

Another import point for this to work properly is to include in your table schema scripts, DROP and CREATE statements.

The easiest way to create these scripts with SQL Server 2008 is:
  1. right-click on the database name in the SQL Server Management Studio
  2. in the flyout menu, select "Tasks" --> "Generate Scripts"
  3. This will take you through a wizard to generate scripts for any or all objects in your database.

Here's what your schema script should look like, this is very simple:

01./****** Object:  Table [dbo].[MyBlogTable]    Script Date: 10/29/2011 13:15:22 ******/
02.IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MyBlogTable]') AND type in (N'U'))
03.DROP TABLE [dbo].[MyBlogTable]
04.GO
05./****** Object:  Table [dbo].[MyBlogTable]    Script Date: 10/29/2011 13:15:22 ******/
06.SET ANSI_NULLS ON
07.GO
08.SET QUOTED_IDENTIFIER ON
09.GO
10.SET ANSI_PADDING ON
11.GO
12.IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MyBlogTable]') AND type in (N'U'))
13.BEGIN
14.CREATE TABLE [dbo].[MyBlogTable](
15.    [Id] [int] IDENTITY(1,1) NOT NULL,
16.    [Title] [varchar](50) NOT NULL,
17.    [Body] [varchar](max) NULL,
18.    [DateCreated] [datetime] NULL,
19. CONSTRAINT [PK_MyBlogTable] PRIMARY KEY CLUSTERED
20.(
21.    [Id] ASC
22.)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
23.) ON [PRIMARY]
24.END
25.GO
26.SET ANSI_PADDING OFF
27.GO


By adding the DROP statements, it helps MvcInstaller run properly in the case that an exception occurs before it has completed.  This way it can simply re-run the sql scripts, drop the tables and re-create them without problems.

So if you experience an exception for some reason and then cannot simply click the "Install" button to re-run the process, check your sql script and make sure it contains the DROP statement with the IF (NOT) EXISTS checks. 

Now you are ready to continue.

This might seem like a lot, but it's really not.  Once these settings are done, then you are ready to deploy your application to your production server, and then install it via a web browser.

Run MvcInstaller

Once your MVC application is deployed to your production server, you are now ready to install the database and membership system via MvcInstaller.  Simply enter "Install" in the browser after the domain name and hit Enter.  The page will look like this.

install view

Before you click the "Install" button let me just go over a few things about this page.

It shows all the information as you have set in the installer.config file, but WITHOUT PASSWORDS!  This is important and a very nice feature.  This means that you can be safe in knowing that you can install your application in front of someone without the fear that they might see your passwords.

The other thing to note is that you'll need to ENABLE WRITE PERMISSIONS, during installation since MvcInstaller makes changes to you web.config file.  After it has been successfully installed, then you can remove the write permissions if you need.

So now we're ready to click "Install" and if this is the first time clicking the button and it's a brand new application, you will see this error message.

install message - rolemanager enabled

This is just telling you that the default ASP.NET MVC web.config configuration for the roleManager section has the enabled attribute set to False.  This means that you intend to create the ASP.NET Membership system, but you need to have the roleManager enabled for MvcInstaller to work properly, so MvcInstaller updates the setting for you and updates all the other provider configuration information.  It also updates the connection string settings.

Now you can simply click the "Install" button again.  This time as long as the installer.config settings aren't violating the web.config Membership attribute settings, you will see a Processing message.

installer processing

Then depending on whether you are also installing your database schema via SQL scripts, it could be instantaneous or a few seconds.  If there aren't any errors, you will see this.

install success

Now you can successfully log onto the application.

logged on

You can see I logged on here as joemanager.

Taking a look at SQL Server we can see that there are no tables.

sql server empty


If I refresh it, it shows that all the tables for the ASP.NET Membership system have been created as well as the database schema for my application.

sql server with tables

If I look at the rows for the MyBlogTable table, I'll see all the data was inserted into the table successfully!

my database schema installed and data inserted

I can also open the Users table and see all the users.

Users table with data

That's it!  My entire database schema and membership are installed with very little effort.

Conclusion

That's MvcInstaller.  The quick and easy way to install your database schema and ASP.NET Membership system for your ASP.NET MVC application on any server.

I hope this NuGet package is helpful to you.

Comments

  • emifont01@gmail.com
    Posted By: Emilio
    On: 10/12/2011 7:15:52 PM

    Hi nice work but i get this error when i click install button "An error occurred executing the configuration section handler for system.web/profile." why ?

  • info@kingwilder.com
    Posted By: King Wilder
    On: 10/12/2011 8:46:42 PM
    @Emilio - sorry about that.  I don't know off hand why you are getting this error.  Do you have that section in your web.config?  Can you send me your web.config?  info@kingwilder.com
  • obioneprivate@hotmail.com
    Posted By: Bibi
    On: 10/31/2011 1:29:45 PM

    Hi, Very Nice job ! Exactly what I need. I've encounter a problem when App_Data contains other files (ASPNETDB.MDF in my case) .

    So I'll Suggest to add searchPattern like "*.sql" when calling Directory.GetFiles() in your RunScripts Static method.

    With this change, It will work like a charm for me !

    Hope this help !

    Thanks again.

    Bibi

  • info@kingwilder.com
    Posted By: King Wilder
    On: 10/31/2011 1:40:08 PM

    @Bibi, unless there is a reason you needed to include the ASPNETDB.MDF file to the App_Data folder, MvcInstaller will add the complete ASP.NET Membership to your database for you.  You don't need to include this.  Is there a reason you needed to include the ASPNETDB.MDF file here?

    Thanks,

    King Wilder

  • obioneprivate@hotmail.com
    Posted By: bibi
    On: 11/1/2011 2:33:27 AM

    @King, Of course not! But it can be many other files or, if you use any other folder for your script, it may be file like readme.txt. So, known that sql script normaly end with ".sql" I think is a good idee to filter file type. But no matter, when you known this limitation.

    Thanks,

    Bibi

  • info@kingwilder.com
    Posted By: King Wilder
    On: 11/1/2011 1:12:52 PM

    @Bibi, it is a good idea and I'll probably make that change in the next update.  In the meantime, you can make sure that your SQL scripts don't interfere with other files in the App_Data folder by just creating a "App_Data/scripts" folder and place your sql scripts in there.  Then just update the installer.config/Path/RelativeSqlPath value to be "App_Data\scripts" (yes, backslash) and it should work with out conflicts.

    Thanks for the suggestion.

    King Wilder

 

User Name:
(Required)
Email:
(Required)