locked
Duplicate foreign keys when renaming ASP.NET Identity tables RRS feed

  • Question

  • User-1011572351 posted

    I am using asp.net identity for authentication and authorization

    I have my User class:

    public class Usuario : IdentityUser<int, ApplicationUserLogin,ApplicationUserRole, ApplicationUserClaim>
      {
      }
      public class ApplicationUserLogin : IdentityUserLogin<int> { }
      public class ApplicationUserClaim : IdentityUserClaim<int> { }
      public class ApplicationUserRole : IdentityUserRole<int> { }

    my class Role:

    public class Role : IdentityRole<int, ApplicationUserRole>,IRole<int>
      {
      }

    In my DbContext I have a class Configurations for Usuario 

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
          base.OnModelCreating(modelBuilder);
         // Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppDbContext, Configuration>());
    
          modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    
          //Initial
          modelBuilder.Configurations.Add(new UsuarioConfiguration());
    }
    
    and my configrations:
    
      internal class UsuarioConfiguration : EntityTypeConfiguration<Usuario>
      {
        public UsuarioConfiguration()
        {
          Property(p => p.Id).HasColumnName("Usuario_Id");
        }
      }
    

     

    However this results in two properties for the AspNetUserRoles to User relation - UserId and Usuario_Id

     

    If I use
           modelBuilder.Entity <Usuario> () totable ("Usuario").;

    Does not duplicate the PK

    But if I use EntityTypeConfiguration <Usuario> it duplicates

    Wednesday, September 24, 2014 10:31 AM

Answers

  • User-734925760 posted

    Hi,

    According to your description and the code you provided, if you want to use modelBuilder.Entity to configure a Many-to-Many Relationship, you need to use the code below:

    modelBuilder.Entity<XXX>() 
        .HasMany() 
        .WithMany() 
        .Map(m => 
        { 
                });

    For more information about ModelBuilder.Entity, please refer to the link below:

    http://msdn.microsoft.com/en-gb/data/jj591620.aspx

    Hope it's useful for you.

    Best Regards,

    Michelle Ge

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 25, 2014 5:10 AM