2017-11-30 6 views
-1

私はasp.net-core 2.0プロジェクトで作業しています。EFコアのOnModelCreatingで1人のユーザーとロールを作成

このプロジェクトには、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クラスでのユーザーの厳しい操作を意味しますか? – Trinity

+0

私のユーザークラスがIdentityUserから派生しているからです。私は、ユーザーを作成する必要があるとき、私はそれを直接挿入することはできません、私はUserManagerの<>とRoleManagerの<>クラス – Bob5421

関連する問題