Modify table Aspnetuserclaims Asp.net Identity

Asked

Viewed 616 times

1

Good morning,

I have a little logic problem with identity.

I need to create an access screen and would like to use the table Claim already made available by it. The big problem is that the table creates a foreign key for the table Aspnetusers, I would like this field to be a foreign key for a table that I will create.

How can I modify this relation between tables?

  • Samuel, I’m not finding your question very clear, what you want is for your new table to have a relationship with Aspnetuserclaims?

  • No friend, I want the relationship that already exists between Claims and users to be erased and create a new one between Claims and another that I will create.

  • But because you want to do this, you would have as per the code of your class?

  • The Userclaims table creates an access for each user, wanted to create an access and several users use msm. Still not have much code beast, not yet created the screens precisely because of this relationship.

  • I believe that the path you are trying to follow is not correct. When you have the classes of your template, edit the question and put them.

  • How can I do it then? I already have the classes of user registration, company registration. I would create the agra access registration screens, but then arose this relationship problem.

Show 2 more comments

1 answer

2


If you are using ASP.NET Identity, the correct one is your Usuario inherit from IdentityUser:

public class Usuario : IdentityUser { ... }

Table naming settings can be changed through the event OnModelCreating using Fluent API:

public class ApplicationDbContext : IdentityDbContext<Usuario>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>()
            .ToTable("Usuarios", "dbo").Property(p => p.Id).HasColumnName("UsuarioId");
        modelBuilder.Entity<Usuario>()
            .ToTable("Usuarios", "dbo").Property(p => p.Id).HasColumnName("UsuarioId");
    }
}

Similarly, this can be done to IdentityUserClaim:

public class UsuarioIdentificacao : IdentityUserClaim { ... }

And the configuration:

        modelBuilder.Entity<IdentityUserClaim>()
            .ToTable("UsuarioIdentificacoes", "dbo").Property(p => p.Id).HasColumnName("UsuarioIdentificacaoId");
        modelBuilder.Entity<UsuarioIdentificacao>()
            .ToTable("UsuarioIdentificacoes", "dbo").Property(p => p.Id).HasColumnName("UsuarioIdentificacaoId");
  • I am doing this, but do not know how to remove Foreign key from Aspnetusersclaims table.

  • But why do you want it? What’s the problem with the Claims table?

  • It is that the table of Claims has a foreign key with the table user, I wanted that this foreign key was with another table that I will create, so several users can use the same Claim. As if it were some kind of job record, then these functions would have the Claims.

  • Yeah, but you just derive the class IdentityUserClaim and put everything there. It’s analogous.

  • That I don’t know how to do it?

  • I’ll update the answer.

  • All right, thank you very much Gypsy!

  • Look now. I think you have it all.

  • Man, that simple, I thought it would be a bit of code. Thank you very much! It helped a lot...

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.