Asp.net core 在OnModelCreating for EF core中创建一个用户和角色

Asp.net core 在OnModelCreating for EF core中创建一个用户和角色,asp.net-core,entity-framework-core,asp.net-core-2.0,Asp.net Core,Entity Framework Core,Asp.net Core 2.0,我正在从事一个asp.net核心2.0项目 该项目包含一个以实体框架为核心的数据库。我正在使用代码优先模式进行迁移 我想做的是在创建结构时在数据库中创建一些默认数据 我需要创建一个用户 我在IdentityDbContext类中查看了OnModelCreating函数,但我无法访问userManager对象 所以我不知道该怎么办。。。。谢谢 这是我的密码: public class MyContext : IdentityDbContext<Utilisateurs>

我正在从事一个asp.net核心2.0项目

该项目包含一个以实体框架为核心的数据库。我正在使用代码优先模式进行迁移

我想做的是在创建结构时在数据库中创建一些默认数据

我需要创建一个用户

我在IdentityDbContext类中查看了OnModelCreating函数,但我无法访问userManager对象

所以我不知道该怎么办。。。。谢谢

这是我的密码:

    public class MyContext : IdentityDbContext<Utilisateurs>
    {
        public static string ConnectionString;

        public MyContext()
        {

        }

        public MyContext(DbContextOptions<pluginwebContext> options) : base(options)
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(MyContext.ConnectionString);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            var user = new MyUserClass { UserName = "test" };

            // Error There
            var result = await _userManager.CreateAsync(user, "1234");
        }

        public DbSet<MyTable1> table1 { get; set; }
        ...
    }
公共类MyContext:IdentityDbContext
{
公共静态字符串连接字符串;
公共MyContext()
{
}
公共MyContext(DbContextOptions):基本(选项)
{
}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
optionsBuilder.UseSqlServer(MyContext.ConnectionString);
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
var user=new MyUserClass{UserName=“test”};
//错误在那里
var result=await_userManager.CreateAsync(用户,“1234”);
}
公共数据库集表1{get;set;}
...
}

在DbInitialize类中初始化数据的约定,而不是在modelCreating方法中。使用modelCreating最常用的方法是将实体映射到数据库表,如下面的代码所示

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
     modelBuilder.Entity<Course>().ToTable("Course");
     modelBuilder.Entity<Enrollment>().ToTable("Enrollment");
     modelBuilder.Entity<Student>().ToTable("Student");
}

是的,但问题是对于用户表,我需要userManager对象。您能解释一下什么是userManager对象吗?你是说usermanager类中的用户crud操作吗?这是因为我的用户类是从IdentityUser派生的。当我需要创建一个用户时,我不能直接插入它,我需要使用UserManager和RoleManager类
  public static class DbInitializer
    {
        public static void Initialize(SchoolContext context)
        {
            //context.Database.EnsureCreated();

            // Look for any students.
            if (context.Students.Any())
                {
                    return;   // DB has been seeded
                }

        var courses = new Course[]
                    {
                        new Course {CourseID = 1050, Title = "Chemistry",      Credits = 3,
                            DepartmentID = departments.Single( s => s.Name == "Engineering").DepartmentID
                        },
                        new Course {CourseID = 4022, Title = "Microeconomics", Credits = 3,
                            DepartmentID = departments.Single( s => s.Name == "Economics").DepartmentID
                        },
                        new Course {CourseID = 4041, Title = "Macroeconomics", Credits = 3,
                            DepartmentID = departments.Single( s => s.Name == "Economics").DepartmentID
                        },
                        new Course {CourseID = 1045, Title = "Calculus",       Credits = 4,
                            DepartmentID = departments.Single( s => s.Name == "Mathematics").DepartmentID
                        },
                        new Course {CourseID = 2042, Title = "Literature",     Credits = 4,
                            DepartmentID = departments.Single( s => s.Name == "English").DepartmentID
                        },
                    };

                    foreach (Course c in courses)
                    {
                        context.Courses.Add(c);
                    }
                    context.SaveChanges();