如何为 Asp.Net 身份自定义 table 名称(例如,从 AspNetUsers 更改为自定义名称)
How to customize table names for Asp.Net Identity (ex. change from AspNetUsers to a custom name)
我创建了一个 Asp.NET Web 应用程序 (.NET Framework) 并选择了 WebAPI 模板和个人帐户进行身份验证。这将是用于对我的 Xamarin 移动应用程序的用户进行身份验证的 WebAPI。
请告诉我如何修改默认的 table 名称,当用户使用 Identity 注册时,用户信息存储在该名称中。默认名称是 AspNetUsers,但我想用自定义名称重命名 table(s)。
是否有更新 table 的任何迁移选项,或者实现此目的的最佳方法是什么?
您可以在 IdentityModel.cs
class
中添加 OnModelCreating
方法
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("CustomName");
}
}
我创建了一个 Asp.NET Web 应用程序 (.NET Framework) 并选择了 WebAPI 模板和个人帐户进行身份验证。这将是用于对我的 Xamarin 移动应用程序的用户进行身份验证的 WebAPI。
请告诉我如何修改默认的 table 名称,当用户使用 Identity 注册时,用户信息存储在该名称中。默认名称是 AspNetUsers,但我想用自定义名称重命名 table(s)。
是否有更新 table 的任何迁移选项,或者实现此目的的最佳方法是什么?
您可以在 IdentityModel.cs
class
OnModelCreating
方法
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("CustomName");
}
}