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.
I've tried to follow the steps from http://k16c.eu/2014/10/12/asp-net-identity-2-0-mariadb/, but when running "Enable-Migrations" a get the following error:
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
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:
<add name="MyConnection" connectionString="Datasource=localhost;Database=test1;uid=user;pwd=password;Convert Zero Datetime=True;Allow User Variables=True;" providerName="MySql.Data.MySqlClient" />
The only code in the project is the two classes ApplicationContext and ApplicationUser:
using System.Data.Entity;
using Microsoft.AspNet.Identity.EntityFramework;
using MySql.Data.Entity;
namespace Models
{
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class ApplicationContext : IdentityDbContext<ApplicationUser>
{
/// <summary>
/// To use this constructor you have to have a connection string
/// name "MyConnection" in your configuration
/// </summary>
public ApplicationContext() : this("MyConnection") { }
/// <summary>
/// Construct a db context
/// </summary>
/// <param name="connStringName">Connection to use for the database</param>
public ApplicationContext(string connStringName) : base(connStringName) { }
/// <summary>
/// Some database fixup / model constraints
/// </summary>
/// <param name="modelBuilder"></param>
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<IdentityRole>()
.Property(c => c.Name)
.HasMaxLength(128)
.IsRequired();
// We have to declare the table name here, otherwise IdentityUser
// will be created
modelBuilder.Entity<IdentityUser>()
.ToTable("AspNetUsers")
.Property(c => c.UserName)
.HasMaxLength(128)
.IsRequired();
#endregion
}
}
}
and
using Microsoft.AspNet.Identity.EntityFramework;
namespace Models
{
public class ApplicationUser : IdentityUser
{
// add whatever properties you want your user to have here
}
}
To make the project compile I also had to add the Nuget package "Microsoft ASP.NET Identity EntityFramework".
The last thing I do is running "Enable-Migrations" and that's when I get the error about conflicting maxlength properties.
I use Visual Studio 2013 update 4 and the latest version of all Nuget packages.
Any ideas how to solve this problem?
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.
I fixed a similar issue by modifying my ApplicationDbContext
's OnModelCreating
method:
from
modelBuilder
.Entity<IdentityUser>()
.Property(p => p.UserName)
.HasMaxLength(255);
to
modelBuilder
.Entity<ApplicationUser>()
.Property(p => p.UserName)
.HasMaxLength(255);