Bin Deploy ASP.NET MVC 3 Application With SQL CE 4.0 & Entity Framework

by prashant 4. July 2011 14:26

A few days back Phil Haack wrote a blog post on how to bin deploy ASP.NET MVC 3 application on the web server where ASP.NET MVC 3 is not installed. Like many users I am also on shared hosting and therefore I do not have full control over IIS or the remote machine so I can install or update ASP.NET MVC 3, SQL Server CE 4.0 and other development related stuff. Phil did an excellent post but that works only if you are deploying application with SQL Server as a database. If you are planning to deploy your application with SQL CE as your application backend then here are the steps you need to perform.

I assume that you have your ASP.NET MVC 3 application ready to be deployed with SQL CE 4.0 and EF 4.1. But make sure you have add assembly and for SQL CE 4.0 using NuGet. I recommend you to use NuGet here because it will add all the necessary configurations in Web.config file and also add the correct references to your project. If you have MVC 3 tools update installed then there is no need to add the references for SQL CE 4.0 and for EF 4.1, the default MVC 3 template will do it for you. But there are still some changes to be done in Web.config file.
 
To deploy your application with SQL CE 4.0 you need to make below changes to the web.config file:
 
Comment or delete everything inside the connectionStrings tag. The connection string by default is pointing to your default SQL Server instance and as we are going to deploy our application with SQL CE 4.0 we need to change the value under connectionStrings tag like the one below.
<add name="ContactEntities" connectionString="Data Source=|DataDirectory|DB.sdf" providerName="System.Data.SqlServerCe.4.0" />
Next is to add DbProviderFactories under System.Data tag. If you run the application on your local development server then it will run without any problems, but if you deploy the web application to the hosting web server where SQL CE 4.0 is not installed then you will get the below error.
 
 To avoid this error you need to manually add the DbProviderFactories in the Web.config file. Below in the markup you need to add to your web.config file.
<system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
</system.data>
The best way to do this and to avoid any unnecessary error is to use NuGet. If you use NuGet to deploy SQL CE 4.0 then it will add all necessary configurations to the web.config file, add the correct dependent assembly to the project.
 
Copy all the dependencies to the BIN folder

The question here is how the one knows what are the dependencies and where to find them all so that they can be deployed with the application? To perform this step you should have Visual Studio 2010 Service Pack 1 (VS 2010 SP1) installed on your machine. After installing VS 2010 SP1 there will be an option called Add Deployable Dependencies added when the user right-click the project icon in the solution explorer. When you click this option a dialog box appears which allows you to select the dependencies required for the project.

If you have read Phil's post by now, you might know the difference of all the above options. Here, as we are deploying SQL CE 4.0 database we will also check SQL Server Compact also. Click OK button to add all the required dependencies. As soon as you click OK button you will notice that a new folder named _bin_deployableAssemblies gets added to the solution explorer.

 

After adding all the dependencies you then need to build you web application which then copies all the dependencies to the project's or web application's bin folder. While deploying the application we will place each file with similar directory structure in the BIN folder on the hosting web server where ASP.NET MVC 3, SQL CE 4.0 and EF 4.1 is not installed. Just in case if you are wondering on how to deploy EF 4.1, then there is no other thing to be done to deploy EF 4.1, a DLL named EntityFramework inside the bin folder will do the work for us.

Note: I have made no changes to the directory structure while placing files at the remote hosting server.

Comments (4)

mark
mark
1/20/2012 7:54:36 PM #

Good so far...but: 1. Where do I copy my xxxx.sdf file in my hosted website (shared server)? 2. Can I choose the location of my xxxx.sdf by setting the |datadirectory| in your example above? If so, how do I set |datadirectory| to point to "~/myData/" Thanks, Mark

Prashant
Prashant
1/21/2012 10:52:31 PM #

1. By default it will be kept inside App_date folder. 2. Yes, You can choose the database file by setting the |datadirectory| If you want to place the database file from other directory then, change the path in the connectionstring in the web.config file.

mark
mark
1/22/2012 12:22:49 AM #

Thanks Prashant, Good article and I appreciate the extra help! --Mark

Melissa
Melissa
1/24/2012 4:37:36 PM #

Brief post on how to deploy ASP.NET MVC3 application on server: http://blog.teamgrowth.net/index.php/net/how-to-deploy-asp-net-mvc3-application-on-the-server Cheers!!

Add Comment

*
*
 

Visit blogadda.com to discover Indian blogs Computers Blogs