<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nathan Bridgewater &#187; MySql</title>
	<atom:link href="http://www.integratedwebsystems.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.integratedwebsystems.com</link>
	<description>My Little .NET Sandbox</description>
	<lastBuildDate>Fri, 10 Sep 2010 16:13:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Walkthrough: Porting Asp.Net MVC Website to Mono 2.6.1 and MySql on Linux Apache &#8211; Porting to Mono Part 3 of 3</title>
		<link>http://www.integratedwebsystems.com/2010/02/walkthrough-porting-asp-net-mvc-website-to-mono-2-6-1-and-mysql-on-linux-apache-porting-to-mono-part-3-of-3/</link>
		<comments>http://www.integratedwebsystems.com/2010/02/walkthrough-porting-asp-net-mvc-website-to-mono-2-6-1-and-mysql-on-linux-apache-porting-to-mono-part-3-of-3/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 04:21:00 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[Mono]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[MySql]]></category>

		<guid isPermaLink="false">http://www.integratedwebsystems.com/?p=447</guid>
		<description><![CDATA[So for the third and final part of this series, I want to walk through how I ported a very small project I posted on CodePlex awhile back called SiteManager. It is an extremely simplified CMS application that I wrote using Linq to Sql and MVC in order to provide a very basic and easy [...]]]></description>
			<content:encoded><![CDATA[<p sizcache="0" sizset="0">So for the third and final part of this series, I want to walk through how I ported a very small project I posted on CodePlex awhile back called <a href="http://sitemanager.codeplex.com" target="_blank">SiteManager</a>. It is an extremely simplified CMS application that I wrote using Linq to Sql and MVC in order to provide a very basic and easy website tool. It uses membership provider for authentication and Linq to Sql Classes with its database in MS Sql Server. For this walkthrough, I’ll be converting the database to MySql and adapting Linq to Sql to Dblinq so it can completely run on Mono and Linux.</p>
</p>
<p> <span id="more-447"></span>
<link rel="stylesheet" type="text/css" href="/css/jquery.lightbox-0.5.css" />
<h3>Index</h3>
<ul sizcache="0" sizset="1">
<li sizcache="0" sizset="1"><a href="#start">Getting Started</a> </li>
<li sizcache="0" sizset="2"><a href="#database">Database Migration</a> </li>
<li sizcache="0" sizset="3"><a href="#l2s">Linq to Sql to Dblinq</a> </li>
<li sizcache="0" sizset="4"><a href="#membership">Membership</a> </li>
<li sizcache="0" sizset="5"><a href="#authorization">Authorization</a> </li>
<li sizcache="0" sizset="6"><a href="#deployment">Deployment</a> </li>
<li sizcache="0" sizset="7"><a href="#vim">VIM (Editing code files on the server)</a> </li>
<li sizcache="0" sizset="8"><a href="#wrapup">Wrap Up</a> </li>
<li sizcache="0" sizset="9"><a href="#source">Get the Source</a> </li>
</ul>
<h3 sizcache="0" sizset="10"><a name="start">Getting Started</a></h3>
<p sizcache="0" sizset="11">Since I’ve already ported this application, it is very easy to identify any of the problem points. But for any new project, I would identify the basic items related to common issues with changing platforms like: data access, authentication, and any operating system specific code. Using the <a href="http://mono-project.com/MoMA" target="_blank">MoMA tool</a> is hugely beneficial to see how your code base might fare in the Mono framework. It will point out any specific code you’re using that could cause problems.&#160;&#160; You should also <a href="http://mono-project.com/Start" target="_blank">browse the Mono site</a> and learn some basics about the framework since they have quite a few useful extended frameworks that are not part of the Windows .Net runtime.&#160; They also have another nice guide out there for <a href="http://www.mono-project.com/Guide:_Porting_ASP.NET_Applications">porting Asp.Net applications</a> where they use PostgreSql and Blog Starter Kit.</p>
<p>&#160;</p>
<h3 sizcache="0" sizset="14"><a name="database">Database Migration</a></h3>
<p>You should know that Microsoft Sql Server works fine with Mono and works well when you are in a Windows environment and want to run Mono applications against existing Sql Servers. Since we’re running this application completely on a Linux server, as you might do in a hosted Linux environment, leaving our database in Sql Server isn’t an option.&#160; I chose to migrate to MySql purely for personal preference since I have a little more experience with this one rather than others like PostgreSql or Sqlite. However with Dblinq, you will have quite a few options.</p>
<h4>Create an Empty MySql Database and an Application User</h4>
<p sizcache="0" sizset="15">If you recall from my first post in this series, we <a href="http://www.integratedwebsystems.com/2010/01/installing-opensuse-11-2-with-mono-2-6-1-and-apache-using-text-mode-configuration-porting-to-mono-part-1-of-3/#InstallMonoAndApache">installed and configured MySql server</a>.&#160;&#160; We also discussed creating new users. You will want to create a new database with a new user who has access to it from the local machine. (Note that this can also be performed with the <a href="http://dev.mysql.com/downloads/workbench/5.2.html" target="_blank">MySql Workbench GUI Tools</a>).</p>
<p>So to begin, using Putty, SSH into your server.&#160; Then run:</p>
<pre class="brush: sql; gutter: false;">root@server: mysql -u admin_username -p
Enter password: ********
create database sitemanager_mono;
grant all privileges on sitemanager_mono.* to 'aspnet'@'localhost' identified by 'pass';
quit</pre>
<p>You may want to replace the username and password I used here with something more secure, but for this demo I kept it simple. You can do that by replacing ‘aspnet’ with a username of your choice and ‘pass’ with a password of your choice.</p>
<p>You now have a new empty database with its own power user account that can only access it from the local machine. This will be our application user so keep the credentials handy for later when we edit the config file.</p>
<h4>Migrating Sql Server Schema to MySql</h4>
<p sizcache="0" sizset="17">I used the <a href="http://dev.mysql.com/downloads/workbench/5.2.html" target="_blank">MySql Migration Toolkit</a> (part of the GUI Workbench) to perform my migration. It actually worked out pretty well with the exception of a few data type conversions. I like to use varchar(max) fields in Sql server. I also use bit fields for booleans since they parse right over to C# booleans.&#160; The migration tool didnt’ like varchar(max) and converted them to varchar(-1). It also converted my bit fields to tinyint(4). So during the migration, I was able to make edits to a few of my tables that contained those fields. For varchar(max), you have a choice. You can: use a smaller field like varchar(5000), use a Text field, or you can choose to store your text data in a binary blob and encode/decode it to UTF8.&#160; For this, application, I went the route of selecting a smaller varchar field for simplicity; but if my content pages were large, I would probably go the route of text. I found some <a href="http://www.pythian.com/news/7129/text-vs-varchar/" target="_blank">interesting discussion in this post on the topic.</a></p>
<p>So in order, here are the steps I took using the migration tool:</p>
<table border="0" cellspacing="0" cellpadding="2" width="561" sizcache="0" sizset="19">
<tbody sizcache="0" sizset="19">
<tr sizcache="0" sizset="19">
<td valign="top" width="263" sizcache="0" sizset="19"><a class="lightbox" title="Step 1" href="/resources/p447/1.jpg" jquery1267071925472="12" jquery1267072108070="12"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 1" border="0" alt="Step 1" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/1.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">&#160;</td>
</tr>
<tr sizcache="0" sizset="20">
<td valign="top" width="263" sizcache="0" sizset="20"><a class="lightbox" title="Step 2" href="/resources/p447/2.jpg" jquery1267071925472="14" jquery1267072108070="14"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 2" border="0" alt="Step 2" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/2.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">
<p>Choose your source database.</p>
</td>
</tr>
<tr sizcache="0" sizset="21">
<td valign="top" width="263" sizcache="0" sizset="21"><a class="lightbox" title="Step 3" href="/resources/p447/3.jpg" jquery1267071925472="16" jquery1267072108070="16"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 3" border="0" alt="Step 3" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/3.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">
<p>Choose your target database.</p>
</td>
</tr>
<tr sizcache="0" sizset="22">
<td valign="top" width="263" sizcache="0" sizset="22"><a class="lightbox" title="Step 4" href="/resources/p447/4.jpg" jquery1267071925472="18" jquery1267072108070="18"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 4" border="0" alt="Step 4" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/4.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Select the source schema. </td>
</tr>
<tr sizcache="0" sizset="23">
<td valign="top" width="263" sizcache="0" sizset="23"><a class="lightbox" title="Step 5" href="/resources/p447/5.jpg" jquery1267071925472="20" jquery1267072108070="20"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="" border="0" alt="" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/5.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Add exclusions. I chose to exclude my membership tables since I configured those to use a different catalog. </td>
</tr>
<tr sizcache="0" sizset="24">
<td valign="top" width="263" sizcache="0" sizset="24"><a class="lightbox" title="Step 6" href="/resources/p447/6.jpg" jquery1267071925472="22" jquery1267072108070="22"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 6" border="0" alt="Step 6" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/6.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Utf-8 was fine for my solution</td>
</tr>
<tr sizcache="0" sizset="25">
<td valign="top" width="263" sizcache="0" sizset="25"><a class="lightbox" title="Step 7" href="/resources/p447/7.jpg" jquery1267071925472="24" jquery1267072108070="24"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 7" border="0" alt="Step 7" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/7.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Mapping error review</td>
</tr>
<tr sizcache="0" sizset="26">
<td valign="top" width="263" sizcache="0" sizset="26"><a class="lightbox" title="Step 8" href="/resources/p447/8.jpg" jquery1267071925472="26" jquery1267072108070="26"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 8" border="0" alt="Step 8" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/8.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Execution step</td>
</tr>
<tr sizcache="0" sizset="27">
<td valign="top" width="263" sizcache="0" sizset="27"><a class="lightbox" title="Step 9" href="/resources/p447/9.jpg" jquery1267071925472="28" jquery1267072108070="28"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 9" border="0" alt="Step 9" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/9.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Execution results</td>
</tr>
<tr sizcache="0" sizset="28">
<td valign="top" width="263" sizcache="0" sizset="28"><a class="lightbox" title="Step 10" href="/resources/p447/10.jpg" jquery1267071925472="30" jquery1267072108070="30"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 10" border="0" alt="Step 10" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/10.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">
<p>Script errors will appear on the last screen.&#160; Make the necessary changes, click Apply Changes, and Recreate Objects for each error.&#160; When finished, click next to finish.</p>
<p>This is where you’ll see varchar(-1) for conversion from varchar(max) fields.&#160; Change this to text or varchar(n).</p>
<p>Note that sql bit fields convert to smallint(4) if you wish to change that back to bit.</p>
</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<h3 sizcache="0" sizset="29"><a name="l2s">Linq to Sql Classes to Dblinq</a></h3>
<p>After fixing errors and executing the script, you should now have your full schema migrated to MySql. Now you need to convert your Linq to Sql to Dblinq. Mono uses Dblinq as its Linq to Sql replacement internally. I chose to manually use DBlinq because I wasn’t sure how much of the Dblinq code was available in Mono.&#160; I downloaded the current version of the Dblinq project from their public Subversion repository.</p>
<p>So the basic idea here is that we’re going to swap out L2S with Dblinq in a nearly transparent way.&#160; We’ll drop the existing DBML file from our project, use DBMetal.exe to regenerate new classes, and then update our DBFactory utility function to create a new data context from the DBlinq side.&#160;&#160; Essentially, this will allow us to re-use the same operational syntax in the Repository class with minimal changes.</p>
<p>For generating new classes, I used the DBMetal utility that comes with Dblinq. Here&#8217;s the batch file I setup to generate my code: </p>
<pre class="brush: bash; gutter: false;">REM: note that the '-sprocs' option is turned on

&quot;DbMetal.exe&quot; --provider=MySql -database:sitemanager_mono -server:carbuncle -user:user -password:pass &quot;-namespace:IWS.SiteManager.Core.Model&quot; -code:Generated.cs -sprocs --pluralize</pre>
<p>After generating the classes, I replaced the DBFactory GetProvider function with this: </p>
<pre class="brush: csharp; gutter: false;">public static Model.SiteManager GetProvider()
{
    var con_string = GetDefaultConnectionString();
    return new Model.SiteManager(new MySql.Data.MySqlClient.MySqlConnection(con_string));
}</pre>
<p>I used a repository class as a helper to access my data model. Each repository class contains its own instance of the data context (in this case, Data.SiteManager). During construction of the repository class, it creates a new instance of the data context so any of the factory functions are able to use it without maintaining their own context. So I replaced all the member variable types for existing data contexts with the new one generated from Dblinq. I also noticed Dblinq had different casing than the original column names.&#160; So where I had columns like, “ModifiedUTC” before, it was “ModifiedUtc” now. This was a pretty painless fix with find/replace.</p>
<p>You’ll notice here that the Linq syntax itself didn’t change at all.</p>
<pre class="brush: csharp; gutter: false;">public class ContentRepository
{
    SiteManager _DB;

    public ContentRepository()
    {
        _DB = DBFactory.GetProvider();
    }

    /// &lt;summary&gt;
    /// gets a page by its id
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;id&quot;&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public Content GetContent(int id)
    {
        return _DB.Contents.Where(o =&gt; o.ID == id).SingleOrDefault();
    }
    /// &lt;summary&gt;
    /// Load a page by its permalink.
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;permalink&quot;&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public Content GetPage(string section, string permalink)
    {
        return _DB.Contents.Where(o =&gt; o.SectionID == section &amp;&amp; o.Permalink == permalink.Trim().ToLower()).SingleOrDefault();
    }
    /// &lt;summary&gt;
    /// gets the configured default page.
    /// &lt;/summary&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public Content GetIndexPage(string section)
    {
        var data = _DB.Sections.Where(o =&gt; o.ID == section).SingleOrDefault();

        Content page = null;

        if (data != null &amp;&amp; data.DefaultContentID.HasValue)
            page = _DB.Contents.Where(o =&gt; o.ID == data.DefaultContentID.Value).SingleOrDefault;

        return page;
    }
    public string GetSectionIDForPage(int id)
    {
        var section = _DB.Contents.Where(o =&gt; o.ID == id).Select(o =&gt; o.SectionID).SingleOrDefault();
        return section;
    }
    public void SaveContent(Content content)
    {
        Section section = null;

        if (_DB.Sections.Where(o =&gt; o.ID == content.SectionID).Count() == 0)
        {
            section = new Section();
            section.ID = content.SectionID;
            section.Description = &quot;Auto-generated section for new page.&quot;;
            _DB.Sections.InsertOnSubmit(section);
            _DB.SubmitChanges();
        }

        //take current version, push it to content history, overwrite current with new.
        Content old = this.GetContent(content.ID);
        if (old != null)
        {
            //archive old version.
            ContentHistory row = new ContentHistory();
            row.ContentID = content.ID;
            row.Body = old.Body;
            row.CreatedUtc = DateTime.UtcNow;
            _DB.ContentHistories.InsertOnSubmit(row);
            _DB.SubmitChanges();

            old.LoadFromExisting(content); //load current values
        }
        else
        {
            old = content;
            _DB.Contents.InsertOnSubmit(old);
        }

        _DB.SubmitChanges();

        if (section != null) //was a new one. set the default content id.
        {
            section.DefaultContentID = content.ID;
            _DB.SubmitChanges();
        }
    }
    public void DeleteContent(int id)
    {
        var data = _DB.Contents.Where(o =&gt; o.ID == id).SingleOrDefault();
        if (data != null)
        {
            //remove conflicts first.
            var section = this.GetSectionByID(data.SectionID);
            if (section != null)
            {
                section.DefaultContentID = null;
                _DB.SubmitChanges();
            }

            _DB.Contents.DeleteOnSubmit(data);
            _DB.SubmitChanges();
        }
    }
}</pre>
<p>At this point, the project should be compilable. You may need to make a few tweaks to get the loose ends tied up.</p>
<p>&#160;</p>
<h3 sizcache="0" sizset="30"><a name="membership">Membership Provider</a></h3>
<p sizcache="0" sizset="31">After following the <a href="http://www.integratedwebsystems.com/2010/02/how-to-setup-and-configure-mysql-membership-provider-6-2-2-porting-to-mono-part-2-of-3/" target="_blank">instructions from the last post</a>, you should get your membership provider setup and configured. For Mono, I think the biggest hang up for me was not being able to use hashed passwords. Alternatively, you can investigate using encrypted passwords. For simplicity, I used clear text passwords here.</p>
<p>&#160;</p>
<h3 sizcache="0" sizset="32"><a name="authorization">Membership Authorization</a></h3>
<p>You should now have a runable MVC application. Almost everything should work with exception to how security applies rules from the web.config.&#160; I noticed Mono doesn’t apply the same security authorization rules to paths for MVC like it does for web forms. I removed &lt;location path=””&gt; tags from my config file and placed the authorization restrictions to my controller actions instead. </p>
<p>So for example this config section would be used to restrict access to all actions in the Manage controller on Windows.</p>
<pre class="brush: xml; gutter: false;">&lt;location path=&quot;Manage&quot;&gt;
    &lt;system.web&gt;
        &lt;authorization&gt;
            &lt;allow roles=&quot;Admins,Editors&quot;/&gt;
            &lt;deny users=&quot;*&quot;/&gt;
        &lt;/authorization&gt;
    &lt;/system.web&gt;
&lt;/location&gt;</pre>
<p>So on Mono, we remove it from web.config and move to the manage controller as an attribute on ALL actions you want to restrict. This can become a maintenance hassle, but they may eventually fix this for MVC in Mono.</p>
<pre class="brush: csharp; gutter: false;">[Authorize(Roles=&quot;Admins,Editors&quot;)]
public ActionResult Index()
{
    return View();
}</pre>
<h3>&#160;</h3>
<h3 sizcache="0" sizset="33"><a name="deployment">Deployment</a></h3>
<p>Don’t forget to make changes to your web.config file. Include the credentials you setup for your database earlier.&#160; Then using Build-&gt;Publish, publish the site to a local folder. Don’t include the App_Data folder and have it delete all files before publishing. Then using FileZilla over SFTP, connect to your server and upload the published website to the web folder you configured. With the latest Mono, the website should automatically reload the new assemblies. If you have any doubt about this, you can always SSH into the server and restart apache.</p>
<p>To restart apache, gain root access using <strong>su</strong> and then: </p>
<pre class="brush: bash; gutter: false;">service apache2 restart</pre>
<p>Optionally, if restarting apache doesn’t seem to work, you can also search and kill off the mono processes:</p>
<pre class="brush: bash; gutter: false;">diabolos:/home/nathan # ps -A | grep mono
 2162 ?        00:00:03 mono
 2164 ?        00:00:08 mono
diabolos:/home/nathan # kill 2162
diabolos:/home/nathan # kill 2164</pre>
<p>The next request to the server will restart these processes as needed. </p>
<p>&#160;</p>
<h3 sizcache="0" sizset="34"><a name="vim">Quick Guide to VIM</a></h3>
<p>Every once in awhile, you may need to edit files that live on the server without going through the whole process of publishing, deploying, etc. To do this, I like to use a tool called VIM. So what is VIM?&#160; For those of you haven’t used it, it’s a simple text editor in Linux that comes with most distributions. You can think of VIM like “edit” for DOS on steroids. The OpenSuse version even has code completion and color-coded syntax, which is very neat.</p>
<p>To open a file with VIM, SSH into your server and run this command:</p>
<p>vi <em>filename</em></p>
<p>When you open the file, there isn’t a menu, but you’ll see ~ on the left and the text of your file.&#160; VIM has modes (much more than I’ll discuss here), but you need to know of at least two:&#160; insert mode and append mode.&#160; This will allow you to insert or append text starting at the cursor position. So move your cursor to any location and press <em><strong>i</strong></em> for insert mode. You will be able to freely type almost anything. To exit insert mode, hit <em><strong>Esc</strong></em>. Append mode uses the same behavior by pressing <em><strong>a</strong></em>. You will append text starting at the cursor position. Entering <strong><em>:d</em></strong> will delete the current line. Be sure not to use arrow keys while in an editing mode.</p>
<p>To save a file, press <strong><em>Esc </em></strong>to exit editing mode, and then press<strong><em> :w</em></strong> to write the file. You can then enter <strong><em>:q</em></strong> to quit.&#160;&#160; And to save and quit, use <strong><em>:wq.</em></strong>&#160; You can get more info by entering <strong><em>man vi</em></strong> at the command prompt.</p>
<p>&#160;</p>
<h3 sizcache="0" sizset="35"><a name="wrapup">Wrap Up</a></h3>
<p>So that’s it. This is a tiny project, but you may be able to apply these ideas to your own projects.&#160; For me, this gets my imagination fired up about what kinds of solutions I can take advantage of using Mono. I think there’s a lot of potential here.&#160; I’ve recently begun using interfaces and dependency injection with my projects for data frameworks to avoid the mess we covered in the Linq to Sql to Dblinq section. This means I can write a decoupled application that can optionally use Linq to Sql or Dblinq as a pluggable assembly that is injected using settings in my config file. This will make the transition between Windows and Mono much less painful.</p>
<p>&#160;</p>
<h3 sizcache="0" sizset="36"><a name="source">Get the Source</a></h3>
<p sizcache="0" sizset="37">To obtain the source, head over to <a href="http://sitemanager.codeplex.com/SourceControl/list/changesets" target="_blank">sitemanager.codeplex.com</a> and download the latest source.&#160; You’ll find the mono ported code under the /branches/mono-mysql-fork branch. </p>
<p>&#160;</p>
<h3>Useful Links</h3>
<ul sizcache="0" sizset="38">
<li sizcache="0" sizset="38"><a href="http://www.mono-project.com" target="_blank">Mono project</a> </li>
<li sizcache="0" sizset="39"><a href="http://mono-project.com/Start" target="_blank">Getting Started with Mono</a> </li>
<li sizcache="0" sizset="40"><a href="http://mono-project.com/Guide:_Porting_ASP.NET_Applications" target="_blank">Porting Asp.Net Applications</a> </li>
<li sizcache="0" sizset="41"><a href="http://dev.mysql.com/downloads/connector/net/" target="_blank">MySql .NET Connector</a> (I downloaded mono build &amp; source) </li>
<li sizcache="0" sizset="42"><a href="https://sitemanager.svn.codeplex.com/svn/branches/mono-mysql-fork" target="_blank">SiteManager CodePlex Mono Branch in Subversion</a> </li>
<li sizcache="0" sizset="43"><a href="http://linq.to/db" target="_blank">Dblinq website</a> </li>
<li sizcache="0" sizset="44"><a href="http://dblinq2007.googlecode.com/svn/trunk/" target="_blank">Dblinq Subversion</a> </li>
</ul>
<p><script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">
		google.load("jquery", "1.4.1");
	</script><script type="text/javascript" src="/js/jquery.lightbox-0.5.pack.js"></script><script type="text/javascript" src="/tools/js/lightbox.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.integratedwebsystems.com/2010/02/walkthrough-porting-asp-net-mvc-website-to-mono-2-6-1-and-mysql-on-linux-apache-porting-to-mono-part-3-of-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Setup and Configure MySql Membership Provider 6.2.2 &#8211; Porting to Mono Part 2 of 3</title>
		<link>http://www.integratedwebsystems.com/2010/02/how-to-setup-and-configure-mysql-membership-provider-6-2-2-porting-to-mono-part-2-of-3/</link>
		<comments>http://www.integratedwebsystems.com/2010/02/how-to-setup-and-configure-mysql-membership-provider-6-2-2-porting-to-mono-part-2-of-3/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 06:00:00 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Configuration Tool]]></category>
		<category><![CDATA[Membership]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[MySql]]></category>
		<category><![CDATA[MySql.Web]]></category>

		<guid isPermaLink="false">http://www.integratedwebsystems.com/?p=448</guid>
		<description><![CDATA[As the second part of this series, I’ll look at building a website that can use the MySql Membership Provider. This is one of the main hang-ups I’ve run into while porting one of my ASP.NET applications to Mono. With the latest MySql Connector, this turns out to be a very easy thing to do. [...]]]></description>
			<content:encoded><![CDATA[<p>As the second part of this series, I’ll look at building a website that can use the MySql Membership Provider. This is one of the main hang-ups I’ve run into while porting one of my ASP.NET applications to Mono. With the latest MySql Connector, this turns out to be a very easy thing to do. It’s nearly as simple as setting up your web.config with the correct parameters and getting the right connector version.</p>
<p> <span id="more-448"></span><br />
<h3>Download the MySql Connector</h3>
<p>Fortunately now, the MySql.Web assembly comes with the prebuilt versions of the <a href="http://dev.mysql.com/downloads/connector/net/" target="_blank">MySql Connector/Net</a>. If you don’t have it, <a href="http://dev.mysql.com/downloads/connector/net/" target="_blank">go get it</a>.&#160; I recommend the Mono build, even for the Windows folks, because it’s a simple zip file with the assemblies. If you would like to see additional samples, download the Source version of their connector. When you have it downloaded, <strong>add a reference to MySql.Web</strong> in your web project.</p>
<h3>&#160;</h3>
<h3>Database</h3>
<p>As with the Microsoft Sql Server membership provider, you have a choice to place your database tables in an existing application database or in its own database. I prefer to use its own just to keep it separated in case I want to share the Membership component among several applications. If you build upon my <a href="http://www.integratedwebsystems.com/2010/01/installing-opensuse-11-2-with-mono-2-6-1-and-apache-using-text-mode-configuration-porting-to-mono-part-1-of-3/" target="_blank">last post</a>, you can simply create an empty database for your membership data.&#160; Don’t create any tables yet. Just create an empty shell and create a user account for the application to connect your database and has access to create and alter tables. If you’re new to MySql, the <a href="http://dev.mysql.com/downloads/workbench/5.2.html" target="_blank">MySql Administration Tools</a> are extremely handy for this.</p>
<p>&#160;</p>
<h3>Configuration</h3>
<p>The configuration options are mostly the same as the ones you’ve seen in the Microsoft Sql Server membership provider. This one has one added feature, which is an autogenerateschema flag.&#160; This flag instructs the membership provider to actually push schema updates based on the version you’re running. To enable it, simply set it to true.&#160; As with other membership configuration, once you have it enabled and configured to connect to your database, it should work immediately.</p>
<p>There are a few caveats that I ran into while configuring this. For example, hashed passwords only works on Windows .Net; not Mono. Encrypted passwords takes a little more configuration and also (through practice here) only seemed to work on Windows .Net.&#160; There may be some bugs in my version of Mono 2.6.1 or maybe another configuration oddity I couldn’t identify.</p>
<p>Nonetheless, the MySql providers work 100% on Windows. If you’re running on Mono, your main option is to use Clear Text passwords; or if you want to get a little creative, download the source code to the MySql Connector and rewire how it runs its encryption through the framework and roll your own instead.</p>
<p>&#160;</p>
<h3>Configuration Tool</h3>
<p>So moving forward, I built a tiny little configuration tool that you can use to generate the web.config sections you need for this. It takes into account some of my experiences with the things I mentioned like Hashed and Encrypted passwords. It will also auto-generate your MachineKey section if you choose to use encrypted passwords on the Windows side.</p>
<p><a class="launch_tool" href="#" jquery1265988263049="49" jquery1265988344594="2">Launch Configuration Tool</a></p>
<p>Here is the default, Windows based configuration that is generated. You may use this and tweak it or generate your own. </p>
<pre class="brush: xml; gutter: false;">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;configuration&gt;
    &lt;connectionStrings&gt;
        &lt;add name=&quot;MySqlMembershipConnection&quot;
            connectionString=&quot;Data Source=server_name;user id=username;password=password;database=database_name;&quot;
            providerName=&quot;MySql.Data.MySqlClient&quot;/&gt;
    &lt;/connectionStrings&gt;

    &lt;system.web&gt;

        &lt;authentication mode=&quot;Forms&quot;&gt;
            &lt;forms
              loginUrl=&quot;~/Account/Logon&quot;

              timeout=&quot;30&quot;
              name=&quot;.ASPXFORM$&quot;
              path=&quot;/&quot;
              requireSSL=&quot;false&quot;
              slidingExpiration=&quot;true&quot;
              defaultUrl=&quot;Default.aspx&quot;

              enableCrossAppRedirects=&quot;false&quot;/&gt;
        &lt;/authentication&gt;

        &lt;membership defaultProvider=&quot;MySqlMembershipProvider&quot;&gt;
            &lt;providers&gt;
                &lt;clear/&gt;
                &lt;add name=&quot;MySqlMembershipProvider&quot;
                      type=&quot;MySql.Web.Security.MySQLMembershipProvider, mysql.web&quot;
                        connectionStringName=&quot;MySqlMembershipConnection&quot;
                        enablePasswordRetrieval=&quot;false&quot;
                        enablePasswordReset=&quot;true&quot;
                        requiresQuestionAndAnswer=&quot;false&quot;
                        requiresUniqueEmail=&quot;true&quot;
                        passwordFormat=&quot;Hashed&quot;
                        maxInvalidPasswordAttempts=&quot;5&quot;
                        minRequiredPasswordLength=&quot;6&quot;
                        minRequiredNonalphanumericCharacters=&quot;0&quot;
                        passwordAttemptWindow=&quot;10&quot;

                        applicationName=&quot;/&quot;
                        autogenerateschema=&quot;true&quot;/&gt;
            &lt;/providers&gt;
        &lt;/membership&gt;

        &lt;roleManager enabled=&quot;true&quot; defaultProvider=&quot;MySqlRoleProvider&quot;&gt;
            &lt;providers&gt;
                &lt;clear /&gt;
                &lt;add connectionStringName=&quot;MySqlMembershipConnection&quot;
                    applicationName=&quot;/&quot;
                    name=&quot;MySqlRoleProvider&quot;
                    type=&quot;MySql.Web.Security.MySQLRoleProvider, mysql.web&quot;
                    autogenerateschema=&quot;true&quot;/&gt;
            &lt;/providers&gt;
        &lt;/roleManager&gt;

        &lt;profile&gt;
            &lt;providers&gt;
                &lt;clear/&gt;
                &lt;add type=&quot;MySql.Web.Security.MySqlProfileProvider, mysql.web&quot;
                      name=&quot;MySqlProfileProvider&quot;
                      applicationName=&quot;/&quot;
                      connectionStringName=&quot;MySqlMembershipConnection&quot;
                      autogenerateschema=&quot;true&quot;/&gt;
            &lt;/providers&gt;
        &lt;/profile&gt;
    &lt;/system.web&gt;
&lt;/configuration&gt;
</pre>
<h3>Testing Membership</h3>
<p>So once you have your membership configured, you’ll need to test it out. First, don&#8217;t forget to update your connection string credentials. Then the easiest way to test your membership configuration (if you are using Visual Studio) is to use the ASP.NET Configuration Tool (appears when selecting your web project). <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="Solution Toolbar" border="0" alt="Solution Toolbar" align="right" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/01/Toolbar.jpg" width="368" height="86" />When membership configuration is working properly the landing page will show you how many users are configured in the system. If there are none, it will still show you zero. You will also have access to add, remove, and update users from this screen.&#160; If it doesn’t show you anything, then there’s likely something wrong in your configuration.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="aspnetConfig" border="0" alt="aspnetConfig" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/01/aspnetConfig.jpg" width="512" height="180" /></p>
<p>&#160;</p>
<p>After the first time testing your membership, you’ll notice all the schema in the database will have been generated and is now filled with data. This was caused by the<em> autogeneratechema</em> flag being set to “true”.&#160; As new versions of MySql.Web assembly are released from MySql, any schema changes new to each version will automatically be applied with this flag enabled. It should also alleviate that “Schema is missing or incorrect” error.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="database" border="0" alt="database" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/01/database.jpg" width="602" height="266" /></p>
<p>&#160;</p>
<h3>Wrap Up</h3>
<p>So with that, you should have your ASP.NET application configured to run membership to a MySql database. If you’re using Mono, this configuration should work with the default ASP.NET MVC web application (after you reference the MySql assemblies and setup the configuration of course).&#160; Look forward to my next post, which will be the final part of this series about porting an ASP.NET MVC application to Mono. We’ll be taking a very simple MVC application called <a href="http://sitemanager.codeplex.com" target="_blank">SiteManager on CodePlex</a> and convert it to run on Mono and MySql.</p>
<p>&#160;</p>
<h3>Useful Links</h3>
<ul>
<li><a href="http://dev.mysql.com/downloads/mysql/" target="_blank">MySql Community Server</a>&#160; &#8211; Installing on OpenSuse Linux? <a href="http://www.integratedwebsystems.com/2010/01/installing-opensuse-11-2-with-mono-2-6-1-and-apache-using-text-mode-configuration-porting-to-mono-part-1-of-3/#InstallMonoAndApache">Refer to my last post</a>. </li>
<li><a href="http://dev.mysql.com/downloads/workbench/5.2.html" target="_blank">MySql GUI Tools</a> </li>
<li><a href="http://dev.mysql.com/downloads/connector/net/" target="_blank">MySql Connector/NET</a> </li>
<li><a class="launch_tool" href="#">My Cool Configuration Tool</a> </li>
</ul>
<p><div id="dialogContent"></div>
<link href="/tools/css/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />
<link href="/tools/css/p448.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="http://www.google.com/jsapi"></script><br />
	<script type="text/javascript">
		google.load("jquery", "1.4.1");
		google.load("jqueryui", "1.7.2");
	</script><br />
	<script language="javascript" type="text/javascript" src="/tools/js/p448.js"></script><br />
	<script src="/tools/js/MicrosoftAjax.js" type="text/javascript"></script><br />
	<script src="/tools/js/MicrosoftMvcAjax.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.integratedwebsystems.com/2010/02/how-to-setup-and-configure-mysql-membership-provider-6-2-2-porting-to-mono-part-2-of-3/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
