2017-11-30 16 views
-1

asp.net-core 2.0 프로젝트에서 작업 중입니다.EF 코어 용 OnModelCreating에서 하나의 사용자 및 역할 만들기

이 프로젝트에는 Entity Framework Core가 포함 된 데이터베이스가 들어 있습니다. 마이그레이션과 함께 코드 우선 모드로 작업하고 있습니다.

내가하고 싶은 일은 구조 생성시 데이터베이스에 기본 데이터를 만드는 것입니다.

사용자를 만들어야합니다.

내 IdentityDbContext 클래스에서 OnModelCreating 함수를 살펴 봤지만 userManager 개체에 액세스 할 수있는 방법이 없습니다. modelCreating 방법에 있지 DbInitialize 클래스에 데이터를 초기화

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; } 
     ... 
    } 

답변

0

협약 :

그래서 내가 할 방법을 모르는

.... 감사합니다

여기 내 코드입니다. modelCreating을 사용하여 주로 사용되는 방법은 다음 코드와 같이 엔티티를 데이터베이스 테이블에 매핑하는 것입니다.

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

나중에 더미 개체에 대한 클래스를 만들 수 있습니다.

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(); 
+0

네, 문제는 사용자 테이블입니다. 나는 userManager 개체가 필요합니다. – Bob5421

+0

userManager 개체를 의미 할 수 있습니까? 너 usermanager 클래스의 사용자 crud 작업을 의미합니까? – Trinity

+0

내 사용자 클래스가 IdentityUser에서 파생 되었기 때문입니다. 사용자를 생성해야 할 때 직접 삽입 할 수 없으며 UserManager <> 및 RoleManager <> 클래스로 작업해야합니다. – Bob5421