Skip to content

Instantly share code, notes, and snippets.

@koistya
Created September 28, 2014 14:11
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 koistya/1e7b0a10224c8932e625 to your computer and use it in GitHub Desktop.
Save koistya/1e7b0a10224c8932e625 to your computer and use it in GitHub Desktop.
ASP.NET Identity
namespace Microsoft.AspNet.Identity.EntityFramework
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.Validation;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
public class IdentityDbContext<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim> : DbContext where TUser: IdentityUser<TKey, TUserLogin, TUserRole, TUserClaim> where TRole: IdentityRole<TKey, TUserRole> where TUserLogin: IdentityUserLogin<TKey> where TUserRole: IdentityUserRole<TKey> where TUserClaim: IdentityUserClaim<TKey>
{
public IdentityDbContext() : this("DefaultConnection")
{
}
public IdentityDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
public IdentityDbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new ArgumentNullException("modelBuilder");
}
EntityTypeConfiguration<TUser> configuration = modelBuilder.Entity<TUser>().ToTable("AspNetUsers");
configuration.HasMany<TUserRole>(u => u.Roles).WithRequired().HasForeignKey<TKey>(ur => ur.UserId);
configuration.HasMany<TUserClaim>(u => u.Claims).WithRequired().HasForeignKey<TKey>(uc => uc.UserId);
configuration.HasMany<TUserLogin>(u => u.Logins).WithRequired().HasForeignKey<TKey>(ul => ul.UserId);
IndexAttribute indexAttribute = new IndexAttribute("UserNameIndex") {
IsUnique = true
};
configuration.Property((Expression<Func<TUser, string>>) (u => u.UserName)).IsRequired().HasMaxLength(0x100).HasColumnAnnotation("Index", new IndexAnnotation(indexAttribute));
configuration.Property((Expression<Func<TUser, string>>) (u => u.Email)).HasMaxLength(0x100);
modelBuilder.Entity<TUserRole>().HasKey(r => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");
modelBuilder.Entity<TUserLogin>().HasKey(l => new { LoginProvider = l.LoginProvider, ProviderKey = l.ProviderKey, UserId = l.UserId }).ToTable("AspNetUserLogins");
modelBuilder.Entity<TUserClaim>().ToTable("AspNetUserClaims");
EntityTypeConfiguration<TRole> configuration2 = modelBuilder.Entity<TRole>().ToTable("AspNetRoles");
IndexAttribute attribute2 = new IndexAttribute("RoleNameIndex") {
IsUnique = true
};
configuration2.Property((Expression<Func<TRole, string>>) (r => r.Name)).IsRequired().HasMaxLength(0x100).HasColumnAnnotation("Index", new IndexAnnotation(attribute2));
configuration2.HasMany<TUserRole>(r => r.Users).WithRequired().HasForeignKey<TKey>(ur => ur.RoleId);
}
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
{
if ((entityEntry != null) && (entityEntry.State == EntityState.Added))
{
List<DbValidationError> source = new List<DbValidationError>();
TUser user = entityEntry.Entity as TUser;
if (user != null)
{
if (this.Users.Any<TUser>(u => string.Equals(u.UserName, user.UserName)))
{
source.Add(new DbValidationError("User", string.Format(CultureInfo.CurrentCulture, IdentityResources.DuplicateUserName, new object[] { user.UserName })));
}
if (this.RequireUniqueEmail && this.Users.Any<TUser>(u => string.Equals(u.Email, user.Email)))
{
source.Add(new DbValidationError("User", string.Format(CultureInfo.CurrentCulture, IdentityResources.DuplicateEmail, new object[] { user.Email })));
}
}
else
{
TRole role = entityEntry.Entity as TRole;
if ((role != null) && this.Roles.Any<TRole>(r => string.Equals(r.Name, role.Name)))
{
source.Add(new DbValidationError("Role", string.Format(CultureInfo.CurrentCulture, IdentityResources.RoleAlreadyExists, new object[] { role.Name })));
}
}
if (source.Any<DbValidationError>())
{
return new DbEntityValidationResult(entityEntry, source);
}
}
return base.ValidateEntity(entityEntry, items);
}
public bool RequireUniqueEmail { get; set; }
public virtual IDbSet<TRole> Roles { get; set; }
public virtual IDbSet<TUser> Users { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment