[英]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中添加一个将NormalizedName
与TenantId
相结合的唯一索引 。 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表
RoleNameIndex
和UserNameIndex
的默认唯一索引。 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.