ASP.NET MVC 4 EF5 with MySQL

asp.net-mvc entity-framework mysql

Question

I just installed Visual Studio 2012, and I want to launch an ASP.NET MVC 4 project using EF5.

I have to use MySQL since my host does not have MSSQL.

How can I instruct my program to utilize MySQL? (I like to use the one from mysql.com or the devart MySQL connector)

1
16
8/29/2012 4:05:04 PM

Accepted Answer

For MySql Connector 6.5.4, you must set up your configuration using a connection string, DbProviderFactory, and a unique DatabaseInitializer. I went through the complete instructions for configuring EF5 and MySql, together with initializer code, are available on my blog. in detail. The following number should be used if you need an ASP.Net membership provider solution: For MySQL, which ASP.NET Membership/Role providers? For a comprehensive EF5 MySql solution, I will also provide the solution here.

A current migration to EF 5 is not supported by the MySql connection, and ASP.NET only supports SimpleMembership (the MVC4 default) on MS SQL, not MySql. For Code First, see the answer down below.

The actions are:

  1. Download EF 5 from NuGet.
  2. Take use of MySql.Data and MySql.Data.Entity from NuGet (6.5.4) or MySql (6.6.4)
  3. Make a MySql Data Provider configuration
  4. Make a MySql Connection String configuration
  5. Build Your Own MySQL Database Initializer
  6. Custom MySql Database Initializer configuration
  7. Set up ASP.NET membership if necessary.

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>

Relationship String

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

Initializer for databases

Using the MySql connection from NuGet (6.5.4) necessitates the use of a custom initializer. Available codes are http://brice-lambson.blogspot.se/2012/05/using-entity-framework-code-first-with.html and http://www.nsilverbullet.net/2012/11/07/6-steps-to-get-entity-framework-5-working-with-mysql-5-5/.

Add this to configuration after that.

<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>

Membership in ASP.NET

<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>

Working on the AccountController and Views

  1. Delete the shared view _LoginPartial, the MVC 4 AccountController, the AccountModels, and the Account view folder.
  2. Make a new web application using MVC 3
  3. Copy the _LogOnPartial shared view, Account view folder, Account models, and Account controller from the MVC 3 application into the MVC 4 application.
  4. Replace @Html.Partial(“_LoginPartial”) in the joint layout view@Html.Partial(“_LogOnPartial”)
26
5/23/2017 12:10:26 PM

Popular Answer

<add name="ConnectionString" providerName="MySql.Data.MySqlClient" connectionString="Data Source=127.0.0.1; port=3306; Initial Catalog=DbName; uid=root; pwd=*Password*;" />


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow