27

Is it possible to setup ASP.NET MVC 2 to work with a MySQL database?

Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
NovaJoe
  • 4,595
  • 6
  • 29
  • 43
  • 3
    Blogging is perfectly fine and outlined in the FAQ, however, you need to ask your question in the question and then post your answer as an answer. This allows people to +/- your answer and post their own answers as well. – Allen Rice Mar 25 '10 at 03:16
  • Good post. I'd say this applies to any asp.net app, not just ASP.NET MVC. – Craig Mar 25 '10 at 03:18
  • Take my edit, take everything after the first line and post it as an answer and then delete it from the question. Also, dont -1 him just because he hasnt figured out the approach to blogging on SO, alot of newbies think its against the rules. He'll figure it out – Allen Rice Mar 25 '10 at 03:24
  • @NovaJoe: SO is not a blog engine, it is a Q&A site. I left your question above and placed the answer below. – RSolberg Mar 25 '10 at 06:07
  • 1
    great post! i don't care if it's Q&A site or blogging site as long as the information I am looking for is THERE. So many times I posted a question and no one replies to it, here I don't need to post, the info is here. My suggestion for RSolberg - stop being so anal, this site exists to provide information, no more no less. +1! – sarsnake Jul 15 '10 at 22:10
  • @sarsnake Agreed, some people here are extremely uptight about the rules here and will enforce them as *they feel is* necessary. I can understand moderating a website, but sometimes I feel as though things are a little over the top here. Still, I enjoy the website and use it quite a bit, despite this, so I can't complain too much about it. – zeboidlund Mar 29 '12 at 04:02

2 Answers2

36

I'm assuming that you have Visual Studio Professional 2008, have access to an instance of MySQL server, and have moderate to advanced development experience. This MAY work with VS2008 Web edition, but not at all sure.

  1. If you haven't, install MySQL Connector for .NET (6.2.2.0 at the time of this write-up)
  2. Optional: install MySQL GUI Tools
  3. If you haven't, install MVC 2 RTM, or better yet, use Microsoft's Web Platform Installer. (UPDATE: MVC 2 has now been released for quite some time)
  4. Create an empty MySQL database. If you don't want to access your application with the MySQL root user account (insecure), create a user account and assign the appropriate privileges (outside the scope of this write-up).
  5. Create a new MVC 2 application in Visual Studio
  6. In the MVC 2 app, reference MySql.Web.dll. It will either be in your GAC, or in the folder that the MySQL Connector installer put it.
  7. Modify the connection strings portion of your web.config:

      <connectionStrings> 
        <remove name="LocalMySqlServer"/> 
        <add name="MySqlMembershipConnection"
             connectionString="Data Source=[MySql server host name];
                               userid=[user];
                               password=[password];
                               database=[database name];" 
             providerName="MySql.Data.MySqlClient"/>
      </connectionStrings>
    

    8.

    Modify the membership portion of your web.config:

      <membership defaultProvider="MySqlMembershipProvider"> 
        <providers>  
          <clear/>  
          <add name="MySqlMembershipProvider"  
               type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, 
                     Version=6.2.2.0, Culture=neutral, 
                     PublicKeyToken=c5687fc88969c44d"  
               connectionStringName="MySqlMembershipConnection"  
               enablePasswordRetrieval="false"  
               enablePasswordReset="true"  
               requiresQuestionAndAnswer="false"  
               requiresUniqueEmail="true"  
               passwordFormat="Hashed"  
               maxInvalidPasswordAttempts="5"  
               minRequiredPasswordLength="6"  
               minRequiredNonalphanumericCharacters="0"  
               passwordAttemptWindow="10"  
               applicationName="/"  
               autogenerateschema="true"/>  
          </providers>  
        </membership>  
    

    9.

    Modify the role manager portion of your web.config:

      <roleManager enabled="true" defaultProvider="MySqlRoleProvider">  
        <providers>  
          <clear />  
          <add connectionStringName="MySqlMembershipConnection"  
               applicationName="/"  
               name="MySqlRoleProvider"  
               type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, 
                     Version=6.2.2.0, Culture=neutral, 
                     PublicKeyToken=c5687fc88969c44d"  
               autogenerateschema="true"/>  
        </providers>  
      </roleManager>
    

    10.

    Modify the profile portion of your web.config:

      <profile>  
        <providers>  
          <clear/>  
          <add type="MySql.Web.Security.MySQLProfileProvider, MySql.Web, 
                     Version=6.2.2.0, Culture=neutral, 
                     PublicKeyToken=c5687fc88969c44d"  
               name="MySqlProfileProvider"  
               applicationName="/"  
               connectionStringName="MySqlMembershipConnection"  
               autogenerateschema="true"/>  
        </providers>  
      </profile>
    

At this point, you ought to be able to run the app and have the default ASP.NET MVC 2 home page come up in your browser. However, it may be a better idea to first run the ASP.NET Web configuration Tool (in Visual Studio top menus: Project -> ASP.NET Configuration). Once the tool launches, check out each of the tabs; no errors = all good.

The configuration tool at Nathan Bridgewater's blog was essential to getting this working. Kudos, Nathan. Look for the "Configuration Tool" heading half way down the page.

The public key token on the MySql.web.dll that I've posted here ought not change any time soon. But in case you suspect a bad token string from copying and pasting or whatever, just use the Visual Studio command line to run: "sn -T [Path\to\your.dll]" in order to get the correct public key token.

There you have it, ASP.NET MVC 2 running over MySQL. Cheers!

NovaJoe
  • 4,595
  • 6
  • 29
  • 43
  • 1
    Thanks NovaJoe! I've recently copied this tool to its own page to make it easier to find. http://www.integratedwebsystems.com/tools-source/ – Nathan Apr 15 '10 at 19:20
  • Noice! And thanks to YOU, my friend. It was an uphill battle and your tool made it possible. Nathan for the win! – NovaJoe Apr 17 '10 at 19:03
  • 2
    Awesome! I think that I will create a new account on StackOverflow to vote up you again! ;) – Click Ok Feb 02 '11 at 20:39
  • @NovaJoe: Thanks mate. Helped a lot! Right now I'm using ASP.NET MVC 4 with MySql.Web version 6.4.4 and this still applies. Quoting your SO profile: yes, iOS and Android development is like the wild west compared to ASP.NET! :D – Leniel Maccaferri Oct 08 '11 at 02:02
  • Awesome, Leniel, so glad to hear it! I never thought this question would be so helpful to so many folks. I'm excited to try it out myself with MVC 3 and upcoming MVC 4. Just gotta find time!! :D – NovaJoe Oct 10 '11 at 16:00
  • Nice to see this is still getting upvotes over two years later! – NovaJoe Sep 30 '13 at 14:53
1

I belive at "10. Modify the profile portion of your web.config::"

<profile>
  <providers>         
    <clear />   ...
      <add type="MySql.Web.Security.MySQLProfileProvider,......

type= has to be: type="MySql.Web.Profile.MySQLProfileProvider"

because in "MySql.Web.Security" I have not found any method MySQLProfileProvider. (but using Version 6.4.4. for .NET 4.0)

And at least, you have to create your own classes for creating the database tables, if there is no ready configured database. Harald

HL1234
  • 11
  • 1
  • Good note. Thanks HL1234. I'm not checking this, but it may have been a typo as well. However, perhaps it's the namespaces that have changed. Not sure. – NovaJoe Nov 03 '11 at 18:23