Korzystam z członkostwa ASP.net od lat i dopiero zaczynam wypróbowywać ASP.net Tożsamość. Właśnie wydali wersję 2.0, która ma obsługiwać klucze podstawowe: int
. Mam zdefiniowane moje niestandardowych klas tożsamość jako, aby uzyskać całkowitą podstawową obsługę klucz:Jak zmienić nazwy tabel dla ASP.net Identity 2.0 z kolumnami identyfikatora int?
public class User : IdentityUser<int, UserLogin, UserRole, UserClaim>
{
}
public class UserLogin : IdentityUserLogin<int>
{
}
public class Role : IdentityRole<int, UserRole>
{
public string Description { get; set; } // Custom description field on roles
}
public class UserRole : IdentityUserRole<int>
{
}
public class UserClaim : IdentityUserClaim<int>
{
}
public class MyDbContext : IdentityDbContext<User, Role, int, UserLogin, UserRole, UserClaim>
{
public MyDbContext() : this("MyDB") { }
public MyDbContext(string connStringName) : base(connStringName)
{
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Role>().ToTable("Roles");
modelBuilder.Entity<UserRole>().ToTable("UserRoles");
modelBuilder.Entity<UserLogin>().ToTable("UserLogins");
modelBuilder.Entity<UserClaim>().ToTable("UserClaims");
base.OnModelCreating(modelBuilder);
}
}
Kiedy tworzę migrację bazy danych, to właściwie utworzy identyfikator kolumny całkowitą dla każdej z tabel tożsamości, jednak ignoruje moje niestandardowe nazwy tabel określone w metodzie OnModelCreating
. Zamiast tego otrzymuję nazwy tabel, takie jak dbo.AspNetUsers
i dbo.AspNetRoles
.
Próbowałem również dodać [Table("TableName")]
do moich niestandardowych klas tożsamości i nie ma to żadnego wpływu.
Patrząc na przykłady dla ASP.NET Identity 1.0, wyglądało na to, że powtarzane każdego z modelBuilder.Entry<Type>().ToTable("TableName")
linii użyciu klas baza Tożsamość:
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<IdentityUser<int, IdentityUserLogin<int>, IdentityUserRole<int>, IdentityUserClaim<int>>>().ToTable("Users");
modelBuilder.Entity<Role>().ToTable("Roles");
modelBuilder.Entity<IdentityRole<int, UserRole>>().ToTable("Roles");
modelBuilder.Entity<UserRole>().ToTable("UserRoles");
modelBuilder.Entity<IdentityUserRole<int>>().ToTable("UserRoles");
modelBuilder.Entity<UserLogin>().ToTable("UserLogins");
modelBuilder.Entity<IdentityUserLogin<int>>().ToTable("UserLogins");
modelBuilder.Entity<UserClaim>().ToTable("UserClaims");
modelBuilder.Entity<IdentityUserClaim<int>>().ToTable("UserClaims");
Jednak próbowałem kilka zmian i moje migracje nie będzie generować . Ciągle otrzymuję błędy podobne do Typ Microsoft.AspNet.Identity.EntityFramework.IdentityUser'4 [System.Int32, Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin'1 [System.Int32], Microsoft.AspNet.Identity.EntityFramework. IdentityUserRole'1 [System.Int32], Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim'1 [System.Int32]] "nie został odwzorowany. Sprawdź, czy typ nie został jawnie wykluczony przy użyciu metody Ignoruj lub NotMappedAttribute adnotacji danych. Sprawdź, czy typ został zdefiniowany jako klasa, nie jest pierwotny ani ogólny i nie dziedziczy po EntityObject.
Jak wygenerować niestandardowe nazwy tabel z kluczami całkowitymi całkowitymi w ASP.net Identity 2.0?
jeśli zdarzy się wylądować tutaj szukając I dentity 3.0, [sprawdź ten q/a] (http://stackoverflow.com/questions/34523066/how-can-i-change-the-table-names-used-by-asp-net-identity-3- vnext). + 1 s dookoła – Terminus