Skip to content

Instantly share code, notes, and snippets.

@pysco68
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pysco68/c447c36dd610c5b111b9 to your computer and use it in GitHub Desktop.
Save pysco68/c447c36dd610c5b111b9 to your computer and use it in GitHub Desktop.
ApplicationUser class
namespace Application.Model
{
using Microsoft.AspNet.Identity.EntityFramework;
public class ApplicationUser : IdentityUser
{
// add whatever properties you want your user to have here
}
}
namespace Application.Model
{
using Microsoft.AspNet.Identity.EntityFramework;
using MySql.Data.Entity;
using System.Data.Entity;
[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<ApplicationUser>()
.ToTable("AspNetUsers")
.Property(c => c.UserName)
.HasMaxLength(128)
.IsRequired();
#endregion
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment