问题描述:

In my project, I use Identity. Identity's default is to create tables in the DB with their names. I want to change these names to my names.

I'm using Fluent Api to configuration tables that created Identity, for example:

public class UserConfiguration : EntityTypeConfiguration<User>

{

public string TableName

{

get { return "Users"; }

}

public UserConfiguration()

{

this.ToTable(TableName);

HasKey(u => u.ProviderId);

}

}

public class UserClaimsConfiguration : EntityTypeConfiguration<IdentityUserClaim>

{

public string TableName

{

get { return "UserClaims"; }

}

public UserClaimsConfiguration()

{

this.ToTable(TableName);

HasKey(u => u.Id);

}

}

In Context, I added configuration above that to DB:

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

base.OnModelCreating(modelBuilder);

modelBuilder.Configurations.Add(new UserConfiguration());

modelBuilder.Configurations.Add(new RolesConfiguration());

modelBuilder.Configurations.Add(new UserClaimsConfiguration());

modelBuilder.Configurations.Add(new UserLoginsConfiguration());

modelBuilder.Configurations.Add(new UserRoleConfiguration());

}

After that I did a migration and Update-Database –TargetMigration: RenameTablesInDB.

But I'm catching an error:

Cannot find the object "dbo.AspNetUserClaims" because it does not exist or you do not have permissions.

网友答案:

I solved my issue. When you redefine identity's table name with use fluent api. You have to set some mandatory settings.

It's row in IdentityRole

this.HasKey<string>(r => r.Id); // It's row decision

Also row in IdentityLoginsConfiguration

HasKey<string>(l => l.UserId);

And IdentityUserRole

HasKey(r => new { r.RoleId, r.UserId });

All together now. I will describe all the action. Follow down..

The first, we have files with configurations our DB. We use EntityTypeConfiguration in our configuration.

EntityTypeConfiguration is an important class in Fluent API. EntityTypeConfiguration provides you important methods to configure entities and its properties to override various Code-First conventions.

Our Classes with configuration:

public class RolesConfiguration : EntityTypeConfiguration<IdentityRole>
{
    public string TableName
    {
        get { return "Roles"; }
    }
    public RolesConfiguration()
    {
        this.ToTable(TableName);
        this.HasKey<string>(r => r.Id); // It's row decision
    }
}

public class UserClaimsConfiguration : EntityTypeConfiguration<IdentityUserClaim>
{
    public string TableName
    {
        get { return "UserClaims"; }
    }
    public UserClaimsConfiguration()
    {
        this.ToTable(TableName);
    }
}

public class UserConfiguration : EntityTypeConfiguration<User>
{
    public string TableName
    {
        get { return "Users"; }
    }
    public UserConfiguration()
    {
        this.ToTable(TableName);
    }
}
 public class UserLoginsConfiguration : EntityTypeConfiguration<IdentityUserLogin>
{
    public string TableName
    {
        get { return "UserLogins"; }
    }

    public UserLoginsConfiguration()
    {
        this.ToTable(TableName);
        HasKey<string>(l => l.UserId); // It's row was my decision
    }
}


public class UserRoleConfiguration : EntityTypeConfiguration<IdentityUserRole>
{
    public string TableName
    {
        get { return "UserRoles"; }
    }

    public UserRoleConfiguration()
    {
        this.ToTable(TableName);
        HasKey(r => new { r.RoleId, r.UserId }); // It's row was my decision
    }
}

Also we have to add configuration to DB:

        modelBuilder.Configurations.Add(new UserConfiguration());
        modelBuilder.Configurations.Add(new RolesConfiguration());
        modelBuilder.Configurations.Add(new UserClaimsConfiguration());
        modelBuilder.Configurations.Add(new UserLoginsConfiguration());
        modelBuilder.Configurations.Add(new UserRoleConfiguration());
        #endregion

        base.OnModelCreating(modelBuilder);

BEFORE I have

After, i added configuration to my DB

网友答案:

Bring down the line base.OnModelCreating(modelBuilder); under

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
{


    modelBuilder.Configurations.Add(new UserConfiguration());
    modelBuilder.Configurations.Add(new RolesConfiguration());
    modelBuilder.Configurations.Add(new UserClaimsConfiguration());
    modelBuilder.Configurations.Add(new UserLoginsConfiguration());
    modelBuilder.Configurations.Add(new UserRoleConfiguration());

        base.OnModelCreating(modelBuilder);

}
相关阅读:
Top