C# 使用 ASP.NET Identity 时如何更改表名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19460386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change the table names when using ASP.NET Identity?
提问by user2315985
I am using the release version (RTM, not RC) of Visual Studio 2013 (downloaded from MSDN 2013-10-18) and therefore the latest (RTM) version of AspNet.Identity. When I create a new web project, I select "Individual User Accounts" for authentication. This creates the following tables:
我使用的是 Visual Studio 2013(从 MSDN 2013-10-18 下载)的发布版本(RTM,而不是 RC),因此是最新的 (RTM) 版本的 AspNet.Identity。当我创建一个新的 Web 项目时,我选择“个人用户帐户”进行身份验证。这将创建以下表:
- AspNetRoles
- AspNetUserClaims
- AspNetUserLogins
- AspNetUserRoles
- AspNetUsers
- AspNet角色
- AspNet 用户声明
- AspNet 用户登录
- AspNet 用户角色
- AspNet用户
When I register a new user (using the default template), these tables (listed above) are created and the AspNetUsers table has a record inserted which contains:
当我注册一个新用户(使用默认模板)时,会创建这些表(上面列出)并且 AspNetUsers 表插入了一条记录,其中包含:
- Id
- UserName
- PasswordHash
- SecurityStamp
- Discriminator
- ID
- 用户名
- 密码哈希
- 安全印章
- 鉴别器
Additionally, by adding public properties to the class "ApplicationUser" I have successfully added additional fields to the AspNetUsers table, such as "FirstName", "LastName", "PhoneNumber", etc.
此外,通过向“ApplicationUser”类添加公共属性,我成功地向 AspNetUsers 表添加了其他字段,例如“FirstName”、“LastName”、“PhoneNumber”等。
Here's my question. Is there a way to change the names of the above tables (when they are first created) or will they always be named with the AspNet
prefix as I listed above? If the table names can be named differently, please explain how.
这是我的问题。有没有办法更改上述表的名称(首次创建时),还是始终使用AspNet
我上面列出的前缀命名?如果表名可以不同地命名,请解释如何命名。
-- UPDATE --
- 更新 -
I implemented @Hao Kung's solution. It does create a new table (for example I called it MyUsers), but it also still creates the AspNetUsers table. The goal is to replace the "AspNetUsers" table with the "MyUsers" table. See code below and database image of tables created.
我实施了@Hao Kung 的解决方案。它确实创建了一个新表(例如我称之为 MyUsers),但它仍然创建了 AspNetUsers 表。目标是用“MyUsers”表替换“AspNetUsers”表。请参阅下面的代码和创建的表的数据库图像。
I would actually like to replace each AspNet
table with my own name... For fxample, MyRoles, MyUserClaims, MyUserLogins, MyUserRoles, and MyUsers.
我实际上想AspNet
用我自己的名字替换每个表...对于 fxample、MyRoles、MyUserClaims、MyUserLogins、MyUserRoles 和 MyUsers。
How do I accomplish this and end up with only one set of tables?
我如何做到这一点并最终只有一组表格?
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string PhonePrimary { get; set; }
public string PhoneSecondary { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(): base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("MyUsers");
}
}
-- UPDATE ANSWER --
-- 更新答案 --
Thanks to both Hao Kung and Peter Stulinski. This solved my problem...
感谢 Hao Kung 和 Peter Stulinski。这解决了我的问题...
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
modelBuilder.Entity<ApplicationUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
modelBuilder.Entity<IdentityUserRole>().ToTable("MyUserRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("MyUserLogins");
modelBuilder.Entity<IdentityUserClaim>().ToTable("MyUserClaims");
modelBuilder.Entity<IdentityRole>().ToTable("MyRoles");
}
采纳答案by Piotr Stulinski
You can do this easily by modifying the IdentityModel.cs as per the below:
您可以通过按照以下方式修改 IdentityModel.cs 来轻松完成此操作:
Override OnModelCreating in your DbContext then add the following, this will change AspNetUser table to "Users" you can also change the field names the default Id column will become User_Id.
在您的 DbContext 中覆盖 OnModelCreating 然后添加以下内容,这会将 AspNetUser 表更改为“用户”,您还可以更改默认 Id 列将成为 User_Id 的字段名称。
modelBuilder.Entity<IdentityUser>()
.ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
or simply the below if you want to keep all the standard column names:
或者如果您想保留所有标准列名称,则只需以下内容:
modelBuilder.Entity<IdentityUser>()
.ToTable("Users", "dbo")
Full example below (this should be in your IdentityModel.cs file) i changed my ApplicationUser class to be called User.
下面的完整示例(这应该在您的 IdentityModel.cs 文件中)我将 ApplicationUser 类更改为 User。
public class User : IdentityUser
{
public string PasswordOld { get; set; }
public DateTime DateCreated { get; set; }
public bool Activated { get; set; }
public bool UserRole { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>()
.ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
modelBuilder.Entity<User>()
.ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
}
}
Please note i have not managed to get this working if the current table exists. Also note whatever columns you do not map the default ones will be created.
请注意,如果当前表存在,我还没有设法让它工作。另请注意,您未映射的任何列都会创建默认列。
Hope that helps.
希望有帮助。
回答by Hao Kung
You can try overriding this method in your DbContext class to map it to a table of your choosing:
您可以尝试在 DbContext 类中覆盖此方法以将其映射到您选择的表:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<IdentityUser>()
.ToTable("AspNetUsers");
回答by Frank Myat Thu
Below is my working solution:
以下是我的工作解决方案:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); // This needs to go before the other rules!
modelBuilder.Entity<ApplicationUser>().ToTable("User");
modelBuilder.Entity<IdentityRole>().ToTable("Role");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
See thisfor more detail
请参阅此了解更多详情
回答by Arvand
We can change asp.net Identity default table names like this:
我们可以像这样更改 asp.net Identity 默认表名:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(): base("DefaultConnection")
{
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("user");
modelBuilder.Entity<ApplicationUser>().ToTable("user");
modelBuilder.Entity<IdentityRole>().ToTable("role");
modelBuilder.Entity<IdentityUserRole>().ToTable("userrole");
modelBuilder.Entity<IdentityUserClaim>().ToTable("userclaim");
modelBuilder.Entity<IdentityUserLogin>().ToTable("userlogin");
}
}
Furthermore we can extend each class and add any property to classes like 'IdentityUser', 'IdentityRole', ...
此外,我们可以扩展每个类并将任何属性添加到诸如“IdentityUser”、“IdentityRole”、...
public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
public ApplicationRole()
{
this.Id = Guid.NewGuid().ToString();
}
public ApplicationRole(string name)
: this()
{
this.Name = name;
}
// Add any custom Role properties/code here
}
// Must be expressed in terms of our custom types:
public class ApplicationDbContext
: IdentityDbContext<ApplicationUser, ApplicationRole,
string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
static ApplicationDbContext()
{
Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
// Add additional items here as needed
}
To save time we can use AspNet Identity 2.0 Extensible Project Templateto extend all the classes.
为了节省时间,我们可以使用AspNet Identity 2.0 可扩展项目模板来扩展所有类。
回答by Manish
You can also create configuration classes and specify every detail of each of your Identity classes, for example:
您还可以创建配置类并指定每个 Identity 类的每个细节,例如:
using System.Data.Entity.ModelConfiguration;
public class ApplicationUserConfig : EntityTypeConfiguration<ApplicationUser>
{
public UserConfig()
{
ToTable("Users");
Property(u => u.LocationName).IsRequired();
}
}
And then include these configurations in the OnModelCreating() method:
然后在 OnModelCreating() 方法中包含这些配置:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new ApplicationUserConfig());
...
}
This will give you complete control over every aspect of the Identity classes.
这将使您完全控制 Identity 类的各个方面。
回答by dnxit
But it does not work in .NET CORE (MVC 6) for that we need to change binding to
但它在 .NET CORE (MVC 6) 中不起作用,因为我们需要将绑定更改为
like
喜欢
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<IdentityRole>().ToTable("Role");
builder.Entity<IdentityUser>(entity =>
{
entity.ToTable("User");
entity.Property(p => p.Id).HasColumnName("UserId");
});
}
It might help someone :)
它可能会帮助某人:)
回答by sgrysoft
Just for documentation purpose, for the one who comes to this post on the years anyears on the future, (like me XD), All answers given up my comment are right, but you can simplyfied with this method given by Alexandru Bucur on his blog
仅出于文档目的,对于那些在未来几年中看到这篇文章的人(如我 XD),放弃我的评论的所有答案都是正确的,但是您可以简单地使用 Alexandru Bucur 在他的博客上提供的这种方法
//But this method is not longer supported on netcore > 2.2, so I need to fix it
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var table = entityType.Relational().TableName;
if (table.StartsWith("AspNet"))
{
entityType.Relational().TableName = table.Substring(6);
}
};
//This is the functional way on NetCore > 2.2
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var tableName = entityType.GetTableName();
if (tableName.StartsWith("AspNet"))
{
entityType.SetTableName(tableName.Substring(6));
}
}