Create one user and role in OnModelCreating for EF core

asp.net-core asp.net-core-2.0 entity-framework-core

Question

I am working on an asp.net-core 2.0 project.

This project contains a database with Entity Framework Core. I am working on Code First mode with migrations.

What i want to do is to create some default datas in my database on structure creation.

I need to create an User.

I have looked in OnModelCreating function in my IdentityDbContext class but i have no way to access to userManager object.

So i do not know how to do.... Thanks

Here is my code:

    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; }
        ...
    }
1
2
11/30/2017 7:13:59 AM

Popular Answer

The convention of initializing data in DbInitialize class not in modelCreating method. The mostly used way using modelCreating is that mapping your entities to database tables like following code.

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

Later, you can create a class for your dummy object.

  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
11/30/2017 9:10:26 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow