在我的项目中,我使用Identity。 Identity的默认设置是在DB中使用其名称创建表。我想把这些名字改成我的名字。
我使用Fluent Api配置创建Identity的表,例如:
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);
}
}
在上下文中,我将上面的配置添加到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());
}
之后我进行了迁移和Update-Database -TargetMigration:RenameTablesInDB。
但是我发现了一个错误:
无法找到对象&#34; dbo.AspNetUserClaims&#34;因为它不存在或您没有权限。
答案 0 :(得分:1)
我解决了我的问题。 当您使用流畅的api重新定义身份的表名时。您必须设置一些强制设置。
它在IdentityRole中的行
this.HasKey<string>(r => r.Id); // It's row decision
还在IdentityLoginsConfiguration中输入
HasKey<string>(l => l.UserId);
和IdentityUserRole
HasKey(r => new { r.RoleId, r.UserId });
现在一起来。 我将描述所有动作。 跟进..
首先,我们有配置数据库的文件。
我们在配置中使用EntityTypeConfiguration
。
EntityTypeConfiguration
是Fluent API中的重要类。 EntityTypeConfiguration
为您提供了重要的方法来配置实体及其属性以覆盖各种代码优先约定。
我们的配置类:
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
}
}
另外,我们必须向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);
我有
之前
之后,我将配置添加到我的数据库
答案 1 :(得分:0)
打开行base.OnModelCreating(modelBuilder);下
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);
}