简体   繁体   English

覆盖AspNetCore.Identity表中的默认索引

[英]Override default indexes in AspNetCore.Identity tables

I'm developing a multi-tenant application in ASP.NET Core 2.1 . 我正在ASP.NET Core 2.1开发一个多租户应用程序。 I'm utilizing AspNetCore.Identity.EntityFrameworkCore framework for user management. 我正在使用AspNetCore.Identity.EntityFrameworkCore框架进行用户管理。 I want to add a unique index combining NormalizedName with TenantId in Role Table. 我想在Role Table中添加一个将NormalizedNameTenantId相结合的唯一索引 Also, in the user table NormalizedUserName with TenantId in User table. 此外,在User表中使用TenantId的用户表NormalizedUserName

This doesn't let me create that index since identity creates a default unique indexes for Role table RoleNameIndex and UserNameIndex for User table. 这不允许我创建该索引,因为identity为Role表创建了Role表RoleNameIndexUserNameIndex的默认唯一索引。 What is the best way to configure that in OnModelCreating method in EF Core? 在EF Core中的OnModelCreating方法中配置它的最佳方法是什么?

 modelBuilder.Entity<User>().HasIndex(u => new { u.NormalizedUserName, u.TenantId }).HasName("UserNameIndex").IsUnique(true);
 modelBuilder.Entity<Role>().HasIndex(u => new { u.NormalizedName, u.TenantId }).HasName("RoleNameIndex").IsUnique(true);

The fluent API currently does not provide a way to remove a previously defined index (like the indexes in question defined by the base OnModelCreating ), but mutable entity metadata does via IMutableEntityType.RemoveIndex method. 流畅的API目前不提供删除先前定义的索引的方法(如基于OnModelCreating定义的索引),但可变实体元数据通过IMutableEntityType.RemoveIndex方法执行。

It can be used like this: 它可以像这样使用:

modelBuilder.Entity<User>(builder =>
{
    builder.Metadata.RemoveIndex(new[] { builder.Property(u => u.NormalizedUserName).Metadata });

    builder.HasIndex(u => new { u.NormalizedUserName, u.TenantId }).HasName("UserNameIndex").IsUnique();
});

modelBuilder.Entity<Role>(builder =>
{
    builder.Metadata.RemoveIndex(new[] { builder.Property(r => r.NormalizedName).Metadata });

    builder.HasIndex(r => new { r.NormalizedName, r.TenantId }).HasName("RoleNameIndex").IsUnique();
});

Don't think you can actually, unless you don't call base.OnModelCreating in your override. 不要以为你实际上可以,除非你没有调用base.OnModelCreating你的覆盖。 However, you'd then be responsible for all the Identity fluent config. 但是,您将负责所有Identity fluent配置。 You can see what you're dealing with here on Github . 您可以在Github上看到您正在处理的内容。

The only thing to be aware of, if you go down that path, is that you'll need to watch for changes to the fluent config and make sure you add those into yours as they are introduced. 唯一需要注意的是,如果沿着这条路走下去,那就是你需要注意对流畅配置的更改,并确保在引入时将它们添加到你的配置中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AspNetCore.Identity中的Google身份验证 - Google authentication in AspNetCore.Identity 将 AddPooledDbContextFactory 与 AspNetCore.Identity 一起使用 - Using AddPooledDbContextFactory with AspNetCore.Identity AspNetCore.Identity LockoutOptions.AllowedForNewUsers属性 - AspNetCore.Identity LockoutOptions.AllowedForNewUsers Property ViewComponent 无法识别 AspNetCore.Identity“用户” - ViewComponent does not recognize AspNetCore.Identity "User" 在界面中使用 AspNetCore.Identity RoleManager 和 UserManager - Use AspNetCore.Identity RoleManager and UserManager in an Interface 身份服务器 4 + AspNetCore.Identity:X 尝试后如何锁定? - identity server 4 + AspNetCore.Identity: how to lockout after X tries? AspNetCore.Identity 不适用于自定义用户/角色实现 - AspNetCore.Identity not working with custom User/Role implementation AspNetCore.Identity UserManager CreateAsync无法保存关系 - AspNetCore.Identity UserManager CreateAsync cannot save relationships AspNetCore.Identity重置密码-无法跟踪“ X”的实例 - AspNetCore.Identity Reset Password - Instance of 'X' cannot be tracked AspNetCore.Identity RemoveFromRoleAsync 不会从用户中删除角色 - AspNetCore.Identity RemoveFromRoleAsync does not remove role from user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM