When I see at other sample projects, the number of tables created for supporting Identity in the db is great (such ones as AspNetRoles, AspNetUserClaims, etc..), but in my case when I make the migration and the update only the User table has been created. What is the reason? Here is my code in the startup, in the dbcontext and my class user:
当我看到其他示例项目时,为在db中支持Identity而创建的表的数量很大(例如AspNetRoles,AspNetUserClaims等),但在我的情况下,当我进行迁移并且仅更新User表时已经被创造了。是什么原因?这是我在启动时的代码,在dbcontext和我的类用户中:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSession();
services.AddMemoryCache();
services.AddDbContext<ApplicationDbContext>( options =>
options.UseSqlServer(Configuration["Data:photoarchiver:ConnString"]));
services.AddIdentity<User, IdentityRole > (
opts => {
opts.Password.RequireDigit = false;
opts.Password.RequiredLength = 7;
opts.Password.RequireLowercase = true;
opts.Password.RequireUppercase = false;
opts.Password.RequireNonAlphanumeric = false;
}).AddEntityFrameworkStores<ApplicationDbContext>();
}
Class DbContext:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<User>().HasMany(u => u.Photos).WithOne(i => i.User);
modelBuilder.Entity<Photo>().ToTable("Photos");
modelBuilder.Entity<Photo>().HasOne(i => i.User).WithMany(u => u.Photos);
modelBuilder.Entity<Category>().ToTable("Categories");
}
public DbSet<Photo> Photos { get; set; }
public DbSet<Category> Categories { get; set; }
}
Class User:
public class User : IdentityUser
{
public virtual List<Photo> Photos { get; set; }
[Required]
public string DisplayName { get; set; }
public string Notes { get; set; }
[Required]
public DateTime CreatedDate { get; set; }
}
1 个解决方案
#1
2
To get all the AspNetRoles, etc
tables "for free" you need to change your ApplicationDbContext
to extend from IdentityDbContext<User>
instead of just DbContext
. IdentityDbContext<T>
is found in the Microsoft.AspNetCore.Identity.EntityFrameworkCore
namespace. You can see from the source code https://github.com/aspnet/Identity/blob/master/src/EF/IdentityDbContext.cs, IdentityDbContext
will bring in the required DbSet
properties. As you correctly identified in the comment to your question, you will need to call base.OnModelCreating(builder)
and re-make your migration files.
要“免费”获取所有AspNetRoles等表,您需要更改ApplicationDbContext以从IdentityDbContext
#1
2
To get all the AspNetRoles, etc
tables "for free" you need to change your ApplicationDbContext
to extend from IdentityDbContext<User>
instead of just DbContext
. IdentityDbContext<T>
is found in the Microsoft.AspNetCore.Identity.EntityFrameworkCore
namespace. You can see from the source code https://github.com/aspnet/Identity/blob/master/src/EF/IdentityDbContext.cs, IdentityDbContext
will bring in the required DbSet
properties. As you correctly identified in the comment to your question, you will need to call base.OnModelCreating(builder)
and re-make your migration files.
要“免费”获取所有AspNetRoles等表,您需要更改ApplicationDbContext以从IdentityDbContext