Il metodo utilizzato in asp.net identità 2 di modificare i nomi delle tabelle identità non funziona in asp.net identità 3.Come posso cambiare i nomi delle tabelle usati da asp.net identity 3 (vnext)?
5
A
risposta
3
Modificare le entità costruttore in OnModelCreating del vostro ApplicationDbContext, utilizzando il metodo di estensione ForSqlServerToTable per cambiare la tabella desiderata (s) nome.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
builder.Entity<ApplicationUser>().ForSqlServerToTable("Users");
builder.Entity<IdentityUserRole<string>>().ForSqlServerToTable("UserRoles");
builder.Entity<IdentityUserLogin<string>>().ForSqlServerToTable("UserLogins");
builder.Entity<IdentityUserClaim<string>>().ForSqlServerToTable("UserClaims");
builder.Entity<IdentityRole>().ForSqlServerToTable("Roles");
}
}
4
È possibile farlo facilmente cambiando la mappatura un'entità con metodo di estensione ToTable("TableName")
sul OnModelCreating
della vostra DbContext
:
E non c'è bisogno di usare .ForSqlServerToTable()
, basta .ToTable()
dovrebbe funzionare in qualsiasi database.
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<User>().ToTable("Users"); // Your custom IdentityUser class
builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogins");
builder.Entity<IdentityUserToken<string>>().ToTable("UserTokens");
builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaims");
builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaims");
builder.Entity<IdentityRole>().ToTable("Roles");
}
L'unico problema è quello di ricordarsi di utilizzare i farmaci generici con il tipo di identificatore (stringa è di default sul AspNetCore.
lavorato per me con Plain Old 'ToTable' invece di' ForSqlServerToTable'. (sebbene stavo usando l'identità nucleo di asp.net con il progetto di esempio core dotnet quindi, potrebbe non aiutarti) – Terminus