取消OnModel中的HasIndex创建
我正在尝试使用Identity Framework Core配置多租户应用程序。取消OnModel中的HasIndex创建
我已成功创建自定义ApplicationUser以使用TenantId指令覆盖IdentityUser:https://www.scottbrady91.com/ASPNET-Identity/Quick-and-Easy-ASPNET-Identity-Multitenancy(这些指令不适用于Identity Framework核心,但它们有帮助)。
我被困在了创建数据库表的时候。
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Models.User>(entity =>
{
entity.HasIndex(a => new { a.NormalizedUserName, a.TenantId }).HasName("UserNameIndex").IsUnique();
});
}
我与此意图是取代),其在base.OnModelCreating未定义(UserNameIndex使得其上的两列,而不是仅仅NormalizedUsername的索引。但是,这只是导致错误:
The indexes {'NormalizedUserName', 'TenantId'} on 'User' and {'NormalizedUserName'} on 'User' are both mapped to 'Security.AspNetUser.UserNameIndex' but with different columns ({'NormalizedUserName', 'TenantId'} and {'NormalizedUserName'}).
很显然,我想撤消在之前,我将其添加在我的代码base.OnModelCreating()调用创建索引,但无法找到一个方法来做到那。
有没有一种方法可以从ModelBuilder中删除已经在模型创建链上创建的索引?
我发现从https://github.com/aspnet/EntityFrameworkCore/issues/6239
有帮助的解决方案可以使用MetaData.RemoveIndex()
删除已存在
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Models.User>(entity =>
{
// Added these two lines
var index = b.HasIndex(u => new { u.NormalizedUserName }).Metadata;
b.Metadata.RemoveIndex(index.Properties);
entity.HasIndex(a => new { a.NormalizedUserName, a.TenantId }).HasName("UserNameIndex").IsUnique();
});
}
指数