// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Metadata; namespace Microsoft.AspNet.Identity.SqlServer { public class IdentityDbContext : IdentityDbContext { public IdentityDbContext() { } public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } } public class IdentityDbContext : IdentityDbContext where TUser : IdentityUser { public IdentityDbContext() { } public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } } public class IdentityDbContext : DbContext where TUser : IdentityUser where TRole : IdentityRole where TKey : IEquatable { public DbSet Users { get; set; } public DbSet> UserClaims { get; set; } public DbSet> UserLogins { get; set; } public DbSet> UserRoles { get; set; } public DbSet Roles { get; set; } public DbSet> RoleClaims { get; set; } public IdentityDbContext() { } public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } protected override void OnModelCreating(ModelBuilder builder) { builder.Entity(b => { b.Key(u => u.Id); b.ToTable("AspNetUsers"); }); builder.Entity(b => { b.Key(r => r.Id); b.ToTable("AspNetRoles"); }); builder.Entity>(b => { b.Key(uc => uc.Id); b.ManyToOne().ForeignKey(uc => uc.UserId); b.ToTable("AspNetUserClaims"); }); builder.Entity>(b => { b.Key(rc => rc.Id); b.ManyToOne().ForeignKey(rc => rc.RoleId); b.ToTable("AspNetRoleClaims"); }); var userType = builder.Model.GetEntityType(typeof(TUser)); var roleType = builder.Model.GetEntityType(typeof(TRole)); var userClaimType = builder.Model.GetEntityType(typeof(IdentityUserClaim)); var roleClaimType = builder.Model.GetEntityType(typeof(IdentityRoleClaim)); var userRoleType = builder.Model.GetEntityType(typeof(IdentityUserRole)); //var ucfk = userClaimType.GetOrAddForeignKey(userType.GetPrimaryKey(), new[] { userClaimType.GetProperty("UserId") }); //userType.AddNavigation(new Navigation(ucfk, "Claims", false)); //userClaimType.AddNavigation(new Navigation(ucfk, "User", true)); //var urfk = userRoleType.GetOrAddForeignKey(userType.GetPrimaryKey(), new[] { userRoleType.GetProperty("UserId") }); //userType.AddNavigation(new Navigation(urfk, "Roles", false)); //var urfk2 = userRoleType.GetOrAddForeignKey(roleType.GetPrimaryKey(), new[] { userRoleType.GetProperty("RoleId") }); //roleType.AddNavigation(new Navigation(urfk2, "Users", false)); var rcfk = roleClaimType.GetOrAddForeignKey(roleType.GetPrimaryKey(), new[] { roleClaimType.GetProperty("RoleId") }); roleType.AddNavigation(new Navigation(rcfk, "Claims", false)); builder.Entity>(b => { b.Key(r => new { r.UserId, r.RoleId }); b.ToTable("AspNetUserRoles"); }); // Blocks delete currently without cascade //.ForeignKeys(fk => fk.ForeignKey(f => f.UserId)) //.ForeignKeys(fk => fk.ForeignKey(f => f.RoleId)); builder.Entity>(b => { b.Key(l => new { l.LoginProvider, l.ProviderKey }); b.ManyToOne().ForeignKey(uc => uc.UserId); b.ToTable("AspNetUserLogins"); }); } } }