Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo HasIndex in OnModelCreating

I am trying to configure a multi-tenancy application using Identity Framework Core.

I have successfully created a custom ApplicationUser to override IdentityUser with TenantId using instructions here: https://www.scottbrady91.com/ASPNET-Identity/Quick-and-Easy-ASPNET-Identity-Multitenancy (these are instructions are not for Identity Framework Core but they helped).

I am stuck at the point when I am creating the database tables.

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();
    });
}

My intention with this is to replace the UserNameIndex which is defined in base.OnModelCreating() so that it is an index on two columns rather than just NormalizedUsername. But this just results in the error:

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'}).

Obviously I want to undo the Index that is created in the base.OnModelCreating() call before I add it in my code, but can't find a method to do that.

Is there a way to remove an Index from ModelBuilder that has been created further up the model creation chain?

like image 428
Dan Soper Avatar asked Aug 30 '17 16:08

Dan Soper


People also ask

What is HasIndex in Entity Framework?

HasIndex(String[], String)Configures an index on the specified properties and with the given name.

Does Entity Framework have index core?

The Entity Framework Core Fluent API HasIndex method is used to create a database index on the column mapped to the specified entity property. By default, indexes are created for foreign keys and alternate keys.


1 Answers

I have found a solution with help from https://github.com/aspnet/EntityFrameworkCore/issues/6239

You can use MetaData.RemoveIndex() to remove the index that already exists

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();
    });
}
like image 175
Dan Soper Avatar answered Sep 18 '22 09:09

Dan Soper