Coder Social home page Coder Social logo

mysql.aspnetidentity's Introduction

READ ME

When using MySql connector with Entity Framework and ASP.NET Identity, you may encounter the error

Specified key was too long; max key length is 767 bytes

Reason

The error was cause by the fact that the database was created using utf8 collation. MySql or MariaDB store UTF8 string using 3 bytes per character plus 2 bytes for length.

So a varchar(256) would require 770 bytes, which just exceed the max key length of 767 bytes

Most solution I came across tell you to reduce the key length, however, it is unacceptable for field that store email address like ASP.NET IdentityUser because the official specification for email address is 256 ansi characters.

Solution

  1. Tell EF that we're not using Unicode for Username and Email in IdentityUser
  2. Reduce the string length of Name property for IdentityRole. This is acceptable as role name doesn't need to be that long.

3.Override MySql.Data.Entity.MySqlMigrationSqlGenerator class to tell it to use latin1_general_ci for collation.

Usage

  1. Add the following code to your OnModelCreating method
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // email address doesn't need to be in unicode, check it spec
    modelBuilder.Entity<ApplicationUser>().Property(u => u.UserName).IsUnicode(false);
    modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).IsUnicode(false);
    modelBuilder.Entity<IdentityRole>().Property(r => r.Name).HasMaxLength(255);
}
  1. Add the following code to your entityFramework element in web.config or app.config file
<entityFramework codeConfigurationType="MySql.AspNetIdentity.MySqlEFConfiguration, MySql.AspNetIdentity">

Build

To build this solution, you need to add reference to MySql connector.

My SQL connector can be download at http://dev.mysql.com/downloads/connector/net/

mysql.aspnetidentity's People

Contributors

timdinhdotcom avatar

Stargazers

Sandor Martín avatar lastlink avatar  avatar Artur Fernandes avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.