2

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.

4 Answers 4

9

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);
Sign up to request clarification or add additional context in comments.

2 Comments

This is the actual correct answer. The tutorial in OP uses the wrong class, IdentityUser instead of ApplicationUser. Took me almost an hour to figure out why it wasn't working...
Yes. This should be the correct answer, also working with EF 6.1.3.
1

I already answered your question on the blog post you referenced in your question. stianskj is perfectly right with his workaround, it's probably the easiest way to roll back to EF 6.1.1 for now.

There's already an issue posted in the EF bugtracker: http://entityframework.codeplex.com/workitem/2630

The error seems related to a refactoring done in August and another bug is related to it. So EF6 vNext probably will contain a fix.

Comments

0

Try overriding the UserName property in your custom ApplicationUser and specifying the MaxLengthAttribute or StringLengthAttribute.

Comments

0

Try limiting the length of UserName on ApplicationUser to the same as IdentityUser. I had similar problems with EF 6.1.2, try rolling back to 6.1.1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.