Creating MVC 5 Projects

.Creating MVC 5 Projects with Entity Framework

Creating MVC 5 Projects with Entity Framework

I created a solution which used an MVC 5.2.3 project for the web interface and project for the Data Access Layer (DAL) using Entity Framework 6.1.3. The solution was built using Visual Studio 2015 to run under .NET 4.5.2. This page outlines the process I used to create and publish the projects as well as storing the solutions in a subversion repository.

  1. Names used in MVC 5.2.3 Project
    1. After the project is created, it can be difficult to change all the occurrences where the component names are used. I found it helpful to determine the component names before starting the process of creating the project. An example of names to use is:
      1. Solution Location: c:\svn\CathySite
      2. Solution Name: CathySite
      3. Web Project Name: CathySite.Web
      4. DAL Project Name: CathySite.DAL
      5. Connection String Name: CathySiteEntities
      6. Entity Data Model Name: CathySiteModel
      7. Entity Data Model Namespace Name: CathySiteModel
  1. Creating the Solution with an MVC 5.2.3 Project
    1. Using Visual Studio 2015: File, New, Project, ASP.NET Web Application, Name: CathySite.Web, Location: C:\svn\cathysite, Solution name: CathySite.Web. On the next screen, indicate as an "MVC 4.5 Template". Also check to add folders and core referenced for "MVC" (if applicable).
.Solution Explorer with MVC 5.2.3 Project


Solution Explorer with MVC 5.2.3 Project

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net452" />
  <package id="bootstrap" version="3.0.0" targetFramework="net452" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net452" />
  <package id="jQuery" version="1.10.2" targetFramework="net452" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.Facebook" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.Google" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.MicrosoftAccount" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.Twitter" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net452" />
  <package id="Modernizr" version="2.6.2" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
  <package id="Owin" version="1.0" targetFramework="net452" />
  <package id="Respond" version="1.2.0" targetFramework="net452" />
  <package id="WebGrease" version="1.5.2" targetFramework="net452" />
</packages>


Default Packages in MVC 5.2.3 Project

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="CathySiteEntities" connectionString="metadata=res://*/Models.CathySiteModel.csdl|res://*/Models.CathySiteModel.ssdl|res://*/Models.CathySiteModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=xxxxxxxxx;user id=xxxxxx;password=xxxxx;database=xxxxxxxxx;persistsecurityinfo=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="owin:AutomaticAppStartup" value="false" />
  </appSettings>
  <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.5.2" />
      </system.Web>
  -->
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>    
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
			<dependentAssembly>
				<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31BF3856AD364E35" culture="neutral" />
				<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
			</dependentAssembly>
			<dependentAssembly>
				<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31BF3856AD364E35" culture="neutral" />
				<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
			</dependentAssembly>
			<dependentAssembly>
				<assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31BF3856AD364E35" culture="neutral" />
				<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
			</dependentAssembly>
			<dependentAssembly>
				<assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31BF3856AD364E35" culture="neutral" />
				<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
			</dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </providers>
  </entityFramework>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4" compilerOptions="/langversion:5 /nowarn:1659;1699;1701">
          <providerOption name="CompilerVersion" value="v4.0" />
      </compiler>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
</configuration>


Default Web.config in MVC 5.2.3 Project


Note: If not using social authentication, or deploying to a non-IIS web server, the OWIN components can be removed if you only desire a simple "Forms" authentication. The procedure for removing the OWN components is given in the MVC Authetication page. After that procedure, the OWIN packages and assembly bindings will be removed from the packages.config and Web config.


  1. Creating the Data Access Layer Project
    1. The "Data Access Layer" (DAL) keeps the database access loosely coupled to the code. The DAL project can be shared (referenced) by multiple projects within the solution. The DAL also allows other data sources to be more easily used during testing, such as creating data in memory for automated tests.

    2. Visual Studio must be configured to use MySQL. See the article Entity Framework with MySQL in Visual Studio 2015 for the procedure to configure Visual Studio for MySQL, along with the procedure for setting up the DAL project. Once the DAL project is added, the solution explorer and packages.config should look similar to the ones shown below:
.Solution Explorer with MVC and DAL Projects


Solution Explorer with MVC and DAL Projects


1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.1.3" targetFramework="net45" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net45" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net45" developmentDependency="true" />
  <package id="MySql.Data" version="6.9.7" targetFramework="net45" />
  <package id="MySql.Data.Entity" version="6.9.7" targetFramework="net45" />
</packages>


packages.config for DAL Project


  1. Updating NuGet Packages

    After the Web and DAL projects have been created, you may wish to update all the NuGet packages to the latest version. Also, make sure the packages which are used in multiple projects are the same version (such as EntityFramework). Below is a list of the Web project packages after they were upgraded to the latest version:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.5.0.2" targetFramework="net452" />
  <package id="bootstrap" version="3.3.5" targetFramework="net452" />
  <package id="CKEditor" version="3.6.4" targetFramework="net452" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net45" />
  <package id="jQuery" version="2.1.4" targetFramework="net452" />
  <package id="jQuery.Validation" version="1.14.0" targetFramework="net452" />
  <package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.1" targetFramework="net452" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net45" developmentDependency="true" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Modernizr" version="2.8.3" targetFramework="net452" />
  <package id="MySql.Data" version="6.9.8" targetFramework="net452" />
  <package id="MySql.Data.Entity" version="6.9.8" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
  <package id="Respond" version="1.4.2" targetFramework="net452" />
  <package id="WebGrease" version="1.6.0" targetFramework="net452" />
</packages>


Upgraded packages for Web Project

  1. Subversion: Setup, Storing and Accessing the Code.

    Subversion (SVN) is an open source repository commonly used for source code. SVN was founded by CollabNet in 2000 and became an Apache project in 2010. Tortoise SVN is an SVN client running as a Windows shell extension. Many people find the extension easier to use than the command-line-based client which comes with SVN. AnkhSVN is a SVN plugin for Visual Studio. Below is the procedure I used to setup an SVN Server, install the Tortoise SVN shell extension and the AnkhSVN plugin.

.Visual SVN Server
  1. VisualSVN Server
    1. Download the VisualSVN Server installer at https://subversion.apache.org/, then "download" page, then "binary packages" page.
    2. Choose "Visual SVN Server" (https://www.visualsvn.com/downloads/) 64 bit version (3.3.1)
    3. Install File Name: VisualSVN-Server-3.3.1-x64.msi which includes:
      Apache HTTP Server 2.2.29
      Apace Subversion 1.8.13
      Leave Repositories Location set to: "C:\Repositories"
      Pick Standard Version; Change port to 8443 (instead of using port 443 which is SSL port)
      Leave repository location set to C:\Repositories
      Use "Subversion Authentication" for local machine.
    4. Install repository on local machine using "VisualSVN Server Manager" program.
      Right-click repository, Create New Repository, "Regular FSFS Repository",
      Single-project repository (with top-level "trunk", "branches", and "tags" folders.
      Repository https://Kevin-PC/svn/CathySite
      Optional: in properties, set up "Hooks" eg. Require text in comments.
      Create User: (username, password)
    5. Subversion project only has a command-line-based client... so using Tortoise Client for access with a GUI.


.TortoiseSVN
  1. Tortoise SVN Graphical Client
    1. Make sure SVN client is a compatible version with the SVN server:
      ie. "TortoiseSVN 1.8.11 is linked against Subversion 1.8.13
    2. Choose "TortoiseSVN 1.8.11 64-bit
    3. Actually obtained TortoiseSVN client from: https://www.visualsvn.com/visualsvn/download/tortoisesvn/TortoiseSVN-1.8...
    4. To get code into repository, check out to folder where code is installed (ignore warning that folder is not empty), then Add all the files in folder (except .svn) to repository. Then move all files to a Hold folder and do an Update on the folder to create a working copy.


.AnkhSVN Visual Studio Plugin
  1. AnkhSVN Plugin for Visual Studio
    1. Plug in provides access to SVN from insideVisual Studio
    2. Downloaded from: https://ankhsvn.open.collab.net/ file: AnkhSVN 2.5.12582 (AnkhSvn-2.5.12582.msi)
    3. To initially get a project into subversion. Set the repository using SVN Server. Then inside Visual Studio right-click and select "Add Solution to SVN".


  1. Publishing MVC Project to Web Server
    1. An easy method for publishing a web application to a web server is to set up Publishing Profiles using either FTP or the File System. Once set up you can use "One-Click" publishing to republish the application quickly.
    2. Check-in the publishing profiles, along with your solution, to source code control for easy access to the profiles.
    3. Below is the screen for creating/modifying the publishing profile along with the resulting XML file.
.Publishing Profile using FTP


Publishing Profile using FTP


Below is the contents of the "CathySitePublish.pubxml" publishing profile XML file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FTP</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <publishUrl>ftp://web803.discountasp.net:21/</publishUrl>
    <DeleteExistingFiles>False</DeleteExistingFiles>
    <FtpPassiveMode>True</FtpPassiveMode>
    <FtpSitePath />
    <UserName>0133739|cathyharris</UserName>
    <_SavePWD>True</_SavePWD>
  </PropertyGroup>
</Project>



Web.config Transformations and Web Deploy Parameters

When a web application is deployed to a different computing environment configurations such as database connection string, authorizations, and error handling usually need to be changed. Two ways to automate this process are Web.config Transformations and Web Deploy Parameters. A Web.config transformation file contains XML which specifies how to change the base Web.config file. Web deploy parameters are useful when you do not know now the configuration values until deployment. Such as when the application is handed over to an IT administrator for deployment.

Web.config transformations, Web.config (XDT) was introduced in Visual Studio 2010 for Web Application Projects (WAP) only. When you create a web application project, three files are created:

  1. Web.config
  2. Web.Debug.config
  3. Web.Release.config

The Debug and Release files contain XML markup called transformation statements (xdt) which are applied to the Web.config when the build type is set to Debug or Release. AppHarbor provides an online Web.config Transformation Tester which allows you see how your Web.config file will be transformed without having to build the project. The default Web.Release.config is listed below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0"?>

<!-- For more information on using Web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=301874 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
    finds an attribute "name" that has a value of "MyDB".

    <connectionStrings>
      <add name="MyDB"
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
  -->
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <!--
      In the example below, the "Replace" transform will replace the entire
      <customErrors> section of your Web.config file.
      Note that because there is only one customErrors section under the
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>


Web.Release.config





Error | ASP.NET Developer

Error

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /srv/disk9/1218369/www/kcshadow.net/aspnet/includes/common.inc:2748) in drupal_send_headers() (line 1232 of /srv/disk9/1218369/www/kcshadow.net/aspnet/includes/bootstrap.inc).
  • PDOException: SQLSTATE[42000]: Syntax error or access violation: 1142 INSERT command denied to user '1218369_b2cf'@'185.176.40.58' for table 'watchdog': INSERT INTO {watchdog} (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => cron [:db_insert_placeholder_2] => Attempting to re-run cron while it is already running. [:db_insert_placeholder_3] => a:0:{} [:db_insert_placeholder_4] => 4 [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => http://www.kcshadow.net/aspnet/?q=createmvcsolution [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => 54.198.41.76 [:db_insert_placeholder_9] => 1534839481 ) in dblog_watchdog() (line 160 of /srv/disk9/1218369/www/kcshadow.net/aspnet/modules/dblog/dblog.module).
The website encountered an unexpected error. Please try again later.