Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 EF5 with MySQL

So I've just picked up VS2012 and I want to start an ASP.NET MVC 4 app with EF5.

My host does not have MSSQL so I have to use MySQL.

How do I tell my app that it should use MySQL? (I either want to use the devart MySQL connector or the one from mysql.com)

like image 424
kasperhj Avatar asked Aug 29 '12 15:08

kasperhj


1 Answers

You need to setup your config with a connection string, DbProviderFactory and a custom DatabaseInitializer for MySql Connector 6.5.4. I have detailed the full step for getting EF5 and MySql to play, including code for the initializers on my blog. If you require ASP.Net membership provider solution it been asked before: ASP.NET Membership/Role providers for MySQL? I will post the solution here also for a complete EF5 MySql solution.

The MySql connector does not currently support EF 5 migration and ASP.NET only supports SimpleMembership (MVC4 default) on MS SQL not MySql. The solution below is for Code First.

The steps are:

  1. Grab EF 5 from NuGet
  2. Grab MySql.Data and MySql.Data.Entity from NuGet (6.5.4) or MySql (6.6.4)
  3. Configure a MySql Data Provider
  4. Configure a MySql Connection String
  5. Create a Custom MySql Database Initializer
  6. Configure the Custom MySql Database Initializer
  7. Configure ASP.NET membership if you require it

DbProvider

<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" />
 </DbProviderFactories>
</system.data>

Connection String

<connectionStrings>
  <add name="ConnectionStringName" 
    connectionString="Datasource=hostname;Database=schema_name;uid=username;pwd=Pa$$w0rd;" 
    providerName="MySql.Data.MySqlClient" />
</connectionStrings>

Database Initializer

If you are using MySql connector from NuGet (6.5.4) then a custom initializer is required. Code available at http://brice-lambson.blogspot.se/2012/05/using-entity-framework-code-first-with.html or at http://www.nsilverbullet.net/2012/11/07/6-steps-to-get-entity-framework-5-working-with-mysql-5-5/

Then add this to configuration

<configSections>
  <section name="entityFramework" 
    type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, 
    EntityFramework, Version=5.0.0.0, Culture=neutral, 
    PublicKeyToken=b77a5c561934e089" />
</configSections>
<entityFramework>
  <contexts>
      <context type="Namespace.YourContextName, AssemblyName">
         <databaseInitializer 
           type="Namespace.YourChosenInitializer, AssemblyName">
         </databaseInitializer>
      </context>
    </contexts>
    <defaultConnectionFactory 
      type="MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data" />
</entityFramework>

ASP.NET Membership

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

Get the AccountController and Views working:

  1. Delete the MVC 4 AccountController, AccountModels, Account view folder and _LoginPartial shared view
  2. Create a new MVC 3 web application
  3. Copy the MVC 3 AccountController, AccountModels, Account view folder and _LogOnPartial shared view into your MVC 4 application
  4. Replace @Html.Partial(“_LoginPartial”) in the shared _Layout view with @Html.Partial(“_LogOnPartial”)
like image 63
agilejoshua Avatar answered Sep 25 '22 02:09

agilejoshua