title: ASP.NET Identity using MySQL and Entity Framework
body: <p>I would like to use ASP.NET Identity 2 together with Entity Framework using MySQL for storage, but I can't get it to work.</p>
  
  <p>I've tried to follow the steps from <a href="http://k16c.eu/2014/10/12/asp-net-identity-2-0-mariadb/" rel="nofollow">http://k16c.eu/2014/10/12/asp-net-identity-2-0-mariadb/</a>, but when running "Enable-Migrations" a get the following error:</p>
  
  <blockquote>
    <p>Conflicting configuration settings were specified for property
    'UserName' on type
    'Microsoft.AspNet.Identity.EntityFramework.IdentityUser`4[[System.String,
    mscorlib, Version=4.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089],[Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin,
    Microsoft.AspNet.Identity.EntityFramework, Version=2.0.0.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35],[Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole,
    Microsoft.AspNet.Identity.EntityFramework, Version=2.0.0.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35],[Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim,
    Microsoft.AspNet.Identity.EntityFramework, Version=2.0.0.0,
    Culture=neutral, PublicKeyToken=31bf3856ad364e35]]':      MaxLength = 256
    conflicts with MaxLength = 128</p>
  </blockquote>
  
  <p>This is what I've done:
  I've created a new solution with only a class library project. To that project I've added Nuget packages for Entity Framework and MySQL (as in your post above) and modified app.config by adding a connection string:</p>
  
  <pre><code>&lt;add name="MyConnection" connectionString="Datasource=localhost;Database=test1;uid=user;pwd=password;Convert Zero Datetime=True;Allow User Variables=True;" providerName="MySql.Data.MySqlClient" /&gt;
  </code></pre>
  
  <p>The only code in the project is the two classes ApplicationContext and ApplicationUser:</p>
  
  <pre><code>using System.Data.Entity;
  using Microsoft.AspNet.Identity.EntityFramework;
  using MySql.Data.Entity;
  
  namespace Models
  {
      [DbConfigurationType(typeof(MySqlEFConfiguration))]
      public class ApplicationContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          /// &lt;summary&gt;
          /// To use this constructor you have to have a connection string
          /// name "MyConnection" in your configuration
          /// &lt;/summary&gt;
          public ApplicationContext() : this("MyConnection") { }
  
          /// &lt;summary&gt;
          /// Construct a db context
          /// &lt;/summary&gt;
          /// &lt;param name="connStringName"&gt;Connection to use for the database&lt;/param&gt;
          public ApplicationContext(string connStringName) : base(connStringName) { }
  
          /// &lt;summary&gt;
          /// Some database fixup / model constraints
          /// &lt;/summary&gt;
          /// &lt;param name="modelBuilder"&gt;&lt;/param&gt;
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              base.OnModelCreating(modelBuilder);
  
              #region Fix asp.net identity 2.0 tables under MySQL
              // Explanations: primary keys can easily get too long for MySQL's
              // (InnoDB's) stupid 767 bytes limit.
              // With the two following lines we rewrite the generation to keep
              // those columns "short" enough
              modelBuilder.Entity&lt;IdentityRole&gt;()
              .Property(c =&gt; c.Name)
              .HasMaxLength(128)
              .IsRequired();
  
              // We have to declare the table name here, otherwise IdentityUser
              // will be created
              modelBuilder.Entity&lt;IdentityUser&gt;()
              .ToTable("AspNetUsers")
              .Property(c =&gt; c.UserName)
              .HasMaxLength(128)
              .IsRequired();
              #endregion
          }
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>using Microsoft.AspNet.Identity.EntityFramework;
  
  namespace Models
  {
      public class ApplicationUser : IdentityUser
      {
          // add whatever properties you want your user to have here
      }
  }
  </code></pre>
  
  <p>To make the project compile I also had to add the Nuget package "Microsoft ASP.NET Identity EntityFramework".</p>
  
  <p>The last thing I do is running "Enable-Migrations" and that's when I get the error about conflicting maxlength properties.</p>
  
  <p>I use Visual Studio 2013 update 4 and the latest version of all Nuget packages.</p>
  
  <p>Any ideas how to solve this problem?</p>
  
  <p>If there is any other ways to use Identity 2.0 together with Entity Framework and MySQL, please let me know. I don't have to do it exactly like this, I just thought it would be a good way.</p>