2017-08-30 29 views
0

我正在嘗試使用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中刪除已經在模型創建鏈上創建的索引?

回答

1

我發現從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(); 
    }); 
} 
指數