none
Mudar o nome de tabelas e campos do Identity RRS feed

  • Pergunta

  • Olá pessoal!

    Estou tentando alterar o nome de tabelas e campos do ASP.NET Identity, porém, sem sucesso. Eu tenho a seguinte configuração:

    public class ApplicationUser : IdentityUser
        {
            public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
            {
                // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
                var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
                // Add custom user claims here
                return userIdentity;
            }
        }
    
        public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
    
            public ApplicationDbContext()
                : base("DefaultConnection") //, throwIfV1Schema: false
            {
    
            }
    
            public static ApplicationDbContext Create()
            {
                return new ApplicationDbContext();
            }
    
            protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
    
                modelBuilder.Configurations.Add(new IdentityRoleMap());
                modelBuilder.Configurations.Add(new IdentityUserClaimMap());
                modelBuilder.Configurations.Add(new IdentityUserLoginMap());
                modelBuilder.Configurations.Add(new IdentityUserMap());
                modelBuilder.Configurations.Add(new IdentityUserRoleMap());
            }
        }
        public class IdentityUserClaimMap : EntityTypeConfiguration<IdentityUserClaim>
        {
            public IdentityUserClaimMap()
            {
                this.HasKey(t => t.Id);
                this.Property(t => t.UserId).IsRequired().HasMaxLength(128);
                this.ToTable("Custom_UserClaims");
            }
        }
        public class IdentityUserLoginMap : EntityTypeConfiguration<IdentityUserLogin>
        {
            public IdentityUserLoginMap()
            {
                this.HasKey(t => new { t.UserId, t.LoginProvider, t.ProviderKey });
                this.Property(t => t.LoginProvider).IsRequired().HasMaxLength(128);
                this.Property(t => t.ProviderKey).IsRequired().HasMaxLength(128);
                this.Property(t => t.UserId).IsRequired().HasMaxLength(128);
                this.ToTable("Custom_UserLogins");
            }
        }
        public class IdentityRoleMap : EntityTypeConfiguration<IdentityRole>
        {
            public IdentityRoleMap()
            {
                this.HasKey(t => t.Id);
                this.Property(t => t.Id).IsRequired().HasMaxLength(128);
                this.Property(t => t.Name).IsRequired().HasMaxLength(256);
                this.ToTable("Custom_Roles");
                this
                    .HasMany(x => x.Users)
                    .WithOptional()
                    .HasForeignKey(x => x.RoleId);
            }
        }
    
        public class IdentityUserMap : EntityTypeConfiguration<ApplicationUser>
        {
            public IdentityUserMap()
            {
                this.HasKey(t => t.Id);
                this.Property(t => t.Id).IsRequired().HasMaxLength(128);
                this.Property(t => t.Email).HasMaxLength(256);
                this.Property(t => t.UserName).IsRequired().HasMaxLength(256);
                this.ToTable("Custom_Users");
    
                //Nav:Claims: User can have 1-(0-*) claims
                this
                    .HasMany<IdentityUserClaim>(x => x.Claims)
                    .WithRequired()
                    .HasForeignKey(x => x.UserId)
                    .WillCascadeOnDelete();
    
                //Nav:Logins: User can have 1-(0-*) Logins
                this
                    .HasMany<IdentityUserLogin>(x => x.Logins)
                    .WithRequired()
                    .HasForeignKey(x => x.UserId)
                    .WillCascadeOnDelete();
    
                //Nav:Roles:
                //Interestingly, appears not to be using a *-* Map statement:
                this
                    .HasMany<IdentityUserRole>(x => x.Roles)
                    .WithRequired()
                    .HasForeignKey(x => x.UserId)
                    .WillCascadeOnDelete(false);
            }
        }
        public class IdentityUserRoleMap : EntityTypeConfiguration<IdentityUserRole>
        {
            public IdentityUserRoleMap()
            {
                this
                    .ToTable("Custom_UserRoles")
                    .HasKey(x => new { x.UserId, x.RoleId });
            }
        }

    Com essa configuração, tenho o seguinte erro quando executo a Migration:

    A configuration for type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim' has already been added. To reference the existing configuration use the Entity<T>() or ComplexType<T>() methods.


    Se no meu ApplicationDbContext eu herdar de DbContext ao invés de IdentityDbContext, ele executa sem problemas e cria as minhas tabelas e relacionamentos corretamente. 

    Sinceramente, já postei essa dúvida em vários fórums e ninguém soube me responder "/

    Obrigado.

    quarta-feira, 9 de março de 2016 12:32

Respostas

Todas as Respostas