AspNetUsers

    public class CanDooDbContext : DbContextBase<CanDooDbContext>
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            if (modelBuilder == null)
            {
                throw new ArgumentNullException("modelBuilder");
            }

            // Keep this:
            modelBuilder.Entity<IdentityUser>().ToTable("AspNetUsers");

            // Change TUser to CanDoo.Admin.Models.User everywhere else - 
            // IdentityUser and CanDoo.Admin.Models.User essentially 'share' the AspNetUsers Table in the database:
            EntityTypeConfiguration<CanDoo.Admin.Models.User> table =
                modelBuilder.Entity<CanDoo.Admin.Models.User>().ToTable("AspNetUsers");

            table.Property((CanDoo.Admin.Models.User u) => u.UserName).IsRequired();

            // EF won't let us swap out IdentityUserRole for CanDoo.Admin.Models.UserRole here:
            modelBuilder.Entity<CanDoo.Admin.Models.User>().HasMany<IdentityUserRole>((CanDoo.Admin.Models.User u) => u.Roles);
            modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) =>
                new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");

            // Leave this alone:
            EntityTypeConfiguration<IdentityUserLogin> entityTypeConfiguration =
                modelBuilder.Entity<IdentityUserLogin>().HasKey((IdentityUserLogin l) =>
                    new
                    {
                        UserId = l.UserId,
                        LoginProvider = l.LoginProvider,
                        ProviderKey
                            = l.ProviderKey
                    }).ToTable("AspNetUserLogins");

            //entityTypeConfiguration.HasRequired<IdentityUser>((IdentityUserLogin u) => u.User);

            EntityTypeConfiguration<IdentityUserClaim> table1 =
                modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");

            //table1.HasRequired<IdentityUser>((IdentityUserClaim u) => u.User);

            // Add this, so that IdentityRole can share a table with CanDoo.Admin.Models.Role:
            modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");

            // Change these from IdentityRole to CanDoo.Admin.Models.Role:
            //EntityTypeConfiguration<CanDoo.Admin.Models.Role> entityTypeConfiguration1 =
            //    modelBuilder.Entity<CanDoo.Admin.Models.Role>().ToTable("AspNetRoles");

            //entityTypeConfiguration1.Property((CanDoo.Admin.Models.Role r) => r.Name).IsRequired();

            modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
            modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
            modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

            //base.OnModelCreating(modelBuilder);
        }
    }

 

posted @ 2016-06-17 17:08  shiningrise  阅读(433)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css