use*_*985 206 c# asp.net-identity
我正在使用Visual Studio 2013的发布版本(RTM,而不是RC)(从MSDN 2013-10-18下载),因此使用AspNet.Identity的最新版本(RTM).当我创建一个新的Web项目时,我选择"个人用户帐户"进行身份验证.这将创建以下表:
当我注册一个新用户(使用默认模板)时,会创建这些表(如上所列),并且AspNetUsers表中插入了一条记录,其中包含:
此外,通过向"ApplicationUser"类添加公共属性,我已成功向AspNetUsers表添加了其他字段,例如"FirstName","LastName","PhoneNumber"等.
这是我的问题.有没有办法更改上面表格的名称(首次创建时)或者它们是否总是以AspNet
我上面列出的前缀命名?如果表名可以不同的名称,请解释如何.
- 更新 -
我实施了@Hao Kung的解决方案.它确实创建了一个新表(例如我称之为MyUsers),但它仍然创建了AspNetUsers表.目标是用"MyUsers"表替换"AspNetUsers"表.请参阅下面的代码和创建的表的数据库图像.
我实际上想AspNet
用我自己的名字替换每个表...例如,MyRoles,MyUserClaims,MyUserLogins,MyUserRoles和MyUsers.
我如何实现这一目标,最终只有一组表格?
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");
}
}
Run Code Online (Sandbox Code Playgroud)
- 更新答案 -
感谢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");
}
Run Code Online (Sandbox Code Playgroud)
Pio*_*ski 122
您可以通过按照以下方式修改IdentityModel.cs来轻松完成此操作:
在DbContext中覆盖OnModelCreating然后添加以下内容,这会将AspNetUser表更改为"Users",您还可以更改字段名称,默认Id列将成为User_Id.
modelBuilder.Entity<IdentityUser>()
.ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
Run Code Online (Sandbox Code Playgroud)
如果要保留所有标准列名,请简单地使用以下内容:
modelBuilder.Entity<IdentityUser>()
.ToTable("Users", "dbo")
Run Code Online (Sandbox Code Playgroud)
下面的完整示例(这应该在您的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");
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果当前表存在,我还没有成功.另请注意,无法映射任何列,将创建默认列.
希望有所帮助.
Fra*_*Thu 57
以下是我的工作解决方案:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅此
Hao*_*ung 15
您可以尝试在DbContext类中重写此方法,以将其映射到您选择的表:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<IdentityUser>()
.ToTable("AspNetUsers");
Run Code Online (Sandbox Code Playgroud)
但它在 .NET CORE (MVC 6) 中不起作用,因为我们需要将绑定更改为
喜欢
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");
});
}
Run Code Online (Sandbox Code Playgroud)
它可能对某人有帮助:)
仅出于文档目的,对于那些在未来几年内看到这篇文章的人(像我 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));
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
85981 次 |
最近记录: |