Следующий код работает для ASP.NET MVC Core. Вы можете спросить себя, почему вы хотите изменить имена по умолчанию? Что ж, если вы хотите разместить несколько клиентских сайтов с их собственной аутентификацией в одной базе данных, чтобы избежать дополнительных затрат на хостинг, связанных с наличием дополнительных баз данных. Не забудьте удалить существующие сценарии миграции и инициализировать их с изменениями. Надеюсь это поможет:
protected override void OnModelCreating(ModelBuilder builder)
{
const string
IDENTITY_PREFIX = "CLIENT9002",
ASP_NET_USERS = "T0001_Security_User",
ASP_NET_ROLES = "T0002_Security_Role",
ASP_NET_USER_ROLES = "T0003_Security_User_Role",
ASP_NET_USER_LOGIN = "T0004_Security_User_Login",
ASP_NET_USER_CLAIM = "T0005_Security_User_Claim",
ASP_NET_ROLE_CLAIM = "T0006_Security_Role_Claim",
ASP_NET_USER_TOKENS = "T0007_AspNetUserTokens"
;
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);
#region Change Identity Tables
#region User
//aside: The PK and FK need to be renamed in the DB to avoid conflict.
// Also any migration scripts in future also have to be stripped of any reference to Identity Tables
builder.Entity<ApplicationUser>(entity =>
{
entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USERS));
entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USERS)).Property(p => p.Id).HasColumnName("UserId");
entity.HasMany(r => r.Claims).WithOne().HasForeignKey(rc => rc.UserId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "Claims"));
entity.HasMany(r => r.Logins).WithOne().HasForeignKey(rc => rc.UserId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "Logins"));
entity.HasMany(r => r.Roles).WithOne().HasForeignKey(rc => rc.UserId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "Roles"));
entity.HasMany(d => d.recipecreatedby).WithOne(p => p.ApplicationUserCreatedBy).HasForeignKey(d => d.createdby); //.IsRequired();
entity.HasMany(d => d.recipechangedby).WithOne(p => p.ApplicationUserChangedBy).HasForeignKey(d => d.changedby);
entity.HasIndex(e => e.NormalizedEmail).HasName(string.Format("{0}_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "EmailIndex"));
entity.HasIndex(e => e.NormalizedUserName).HasName(string.Format("{0}_{1}_{2}", IDENTITY_PREFIX, ASP_NET_USERS, "UserNameIndex"));
}
);
#endregion
#region Role
builder.Entity<ApplicationRole>(entity =>
{
entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_ROLES));
entity.HasIndex(e => e.NormalizedName).HasName(string.Format("{0}_{1}_{2}", IDENTITY_PREFIX, ASP_NET_ROLES, "RoleNameIndex"));
entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_ROLES));
entity.HasMany(r => r.Claims).WithOne().HasForeignKey(rc => rc.RoleId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_ROLES, "Claims"));
entity.HasMany(r => r.Users).WithOne().HasForeignKey(rc => rc.RoleId).IsRequired().HasConstraintName(string.Format("{0}_FK_{1}_{2}", IDENTITY_PREFIX, ASP_NET_ROLES, "Users"));
}
);
#endregion
#region Role Claims
builder.Entity<IdentityRoleClaim<int>>(entity =>
{
entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_ROLE_CLAIM));
entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_ROLE_CLAIM));
}
);
#endregion
#region User Claims
builder.Entity<IdentityUserClaim<int>>(entity =>
{
entity.HasKey(e => e.Id).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_CLAIM));
entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_CLAIM));
}
);
#endregion
#region User Login
builder.Entity<IdentityUserLogin<int>>(entity =>
{
entity.HasKey(e => new { e.LoginProvider, e.ProviderKey }).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_LOGIN)); ;
entity.Property(e => e.LoginProvider).HasMaxLength(450);
entity.Property(e => e.ProviderKey).HasMaxLength(450);
entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_LOGIN));
});
#endregion
#region User Role
builder.Entity<IdentityUserRole<int>>(entity =>
{
entity.HasKey(e => new { e.UserId, e.RoleId }).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_ROLES));
entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_ROLES));
});
#endregion
#region Tokens
builder.Entity<IdentityUserToken<int>>(entity =>
{
entity.HasKey(e => new { e.UserId, e.LoginProvider, e.Name }).HasName(string.Format("{0}_PK_{1}", IDENTITY_PREFIX, ASP_NET_USER_TOKENS));
entity.ToTable(string.Format("{0}_{1}", IDENTITY_PREFIX, ASP_NET_USER_TOKENS));
});
#endregion
#region Recipes
builder.Entity<recipe>(x =>
{
x.HasKey(t => new { t.recipeid });
x.HasIndex(i => new { i.slugid });
x.HasIndex(i => new { i.name });
// x.HasMany(r => r.Ingredients).WithOne().HasForeignKey(rc => rc.RecipeID).IsRequired();
});
// builder.Entity<Ingredients>().HasKey(t => new { t.IngredientID });
#endregion
#endregion
}
13.01.2017