我想更改ASP.NET Identity 2.0使用的表的名称。我见过各种类似的问题,但没有解决我的问题的方法。例如这种解决方案:http://www.goatly.net/customizing-table-names-for-identitydbcontextwithcustomuser-data-contexts似乎取决于Code First(?)。无论如何,实现OnModelCreating对我没有任何帮助。我基本上已经将AspNetUsers和类似的表重命名为我自己的名称,并希望能够使用它们。我有一个要使用的现有EF上下文(MyContext),因此我对其进行了修改以从IdentityDbContext派生(编辑t4模板),然后在Startup.Auth中具有:
UserManagerFactory = () =>
{
var context = new MyContext();
Database.SetInitializer<MyContext>(new IdentityDbInitializer());
var userStore = new UserStore<IdentityUser>(context){
DisposeContext = true
};
return new UserManager<IdentityUser>(userStore);
};
但是当我尝试登录时出现问题,提示“无法联系服务器”。我想那是因为我的UserStore无法知道上下文中的哪些User表。我希望这是OnModelCreating代码会做的事情,但是我对此感到迷惑。我想UserStore仍在寻找“ AspNetUsers”表,尽管我不知道这些名称从何而来。我也对为上下文修改t4模板感到不安-这是我应该从IdentityDbContext派生的方式吗?
非常感谢任何帮助。
要遵循的几个步骤:
Microsoft.AspNet.Identity.EntityFramework
connection string
添加到您的web.config / app.config现在您必须定义您的自定义Database Context
:
public class MyContext : IdentityDbContext
{
public MyContext()
: base(<connection string name>)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>()
.ToTable("Users");
modelBuilder.Entity<IdentityRole>()
.ToTable("Roles");
modelBuilder.Entity<IdentityUserRole>()
.ToTable("UserRoles");
modelBuilder.Entity<IdentityUserClaim>()
.ToTable("UserClaims");
modelBuilder.Entity<IdentityUserLogin>()
.ToTable("UserLogins");
}
}
您可以看到,我已经使用DbModelBuilder
将所有实体映射到新表。
Enable-Migrations
将使用配置文件Migrations
创建一个文件夹Configuration.cs
。
它应该看起来像这样:
internal sealed class Configuration : DbMigrationsConfiguration<ConsoleApplication1.Models.MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(ConsoleApplication1.Models.MyContext context)
{
}
}
在构造函数中,将属性AutomaticMigrationsEnabled
更改为true
。
Update-Database
它应该运行脚本来创建新表。
您可以自定义实体(和ID),为定义的每个接口创建自定义类。
public class MyUser : IdentityUser<string, MyUserLogin, MyUserRole, MyUserClaim>
{
}
由于您使用的是Owin,所以您可以定义UserStore:
public class MyUserStore: UserStore<MyUser, MyRole, string, MyUserLogin, MyUserRole, MyUserClaim>
{
public MyUserStore(MyContext context)
: base(context)
{
}
}
和您的UserManager实现:
public class ApplicationUserManager : UserManager<ASPNETIdentity2.Models.MyUser, string>
{
public ApplicationUserManager(IUserStore<ASPNETIdentity2.Models.MyUser, string> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new MyUserStore(context.Get<MyContext>()));
manager.UserValidator = new UserValidator<MyUser, string>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
manager.PasswordValidator = new PasswordValidator()
{
RequiredLength = 5,
RequireNonLetterOrDigit = false, // true
// RequireDigit = true,
RequireLowercase = false,
RequireUppercase = false,
};
return (manager);
}
}
并且您的Owin.Startup
应该看起来像这样:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext(MyContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
}
}
如果要查看自定义实现,可以在ASP.NET MVC上使用简单的工作解决方案来检查我的GitHub repository。
更新:
该解决方案中还有另一个项目,它的设置最少。基本上,如果您只想重命名表,则只需要定义上下文(IdentityDbContext)。
可以找到该项目here。
实体框架的身份2假定代码优先。这个项目似乎可以满足我的需求:https://github.com/cbfrank/AspNet.Identity.EntityFramework
有关文档用法,请查看此答案,有一种简单的方法可以做到这一点:
[注意:我在这里不共享代码,因为主持人因为之前曾做过而生气,所以,请转到链接,是的,我比它的愚蠢人知道,但有规则