Пример #1
0
        public static async Task Initialize(ApplicationDbContext context,
                                            UserManager <ApplicationUser> userManager,
                                            RoleManager <IdentityRole> roleManager)
        {
            context.Database.EnsureCreated();

            String adminId1 = "";
            String adminId2 = "";

            string role1 = "Admin";
            string role2 = "Member";

            string password = "******";

            if (await roleManager.FindByNameAsync(role1) == null)
            {
                await roleManager.CreateAsync(new IdentityRole(role1));
            }
            if (await roleManager.FindByNameAsync(role2) == null)
            {
                await roleManager.CreateAsync(new IdentityRole(role2));
            }

            if (await userManager.FindByNameAsync("a") == null)
            {
                var user = new ApplicationUser
                {
                    UserName    = "******",
                    Email       = "[email protected]",
                    FirstName   = "Allen",
                    LastName    = "Aldridge",
                    Street      = "Fake St",
                    City        = "Vancouver",
                    Province    = "BC",
                    PostalCode  = "V5U K8I",
                    Country     = "Canada",
                    PhoneNumber = "6902341234"
                };

                var result = await userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    await userManager.AddPasswordAsync(user, password);

                    await userManager.AddToRoleAsync(user, role1);
                }
                adminId1 = user.Id;
            }

            if (await userManager.FindByNameAsync("b") == null)
            {
                var user = new ApplicationUser
                {
                    UserName    = "******",
                    Email       = "[email protected]",
                    FirstName   = "Bob",
                    LastName    = "Barker",
                    Street      = "Vermont St",
                    City        = "Surrey",
                    Province    = "BC",
                    PostalCode  = "V1P I5T",
                    Country     = "Canada",
                    PhoneNumber = "7788951456"
                };

                var result = await userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    await userManager.AddPasswordAsync(user, password);

                    await userManager.AddToRoleAsync(user, role1);
                }
                adminId2 = user.Id;
            }

            if (await userManager.FindByNameAsync("m") == null)
            {
                var user = new ApplicationUser
                {
                    UserName    = "******",
                    Email       = "[email protected]",
                    FirstName   = "Mike",
                    LastName    = "Myers",
                    Street      = "Yew St",
                    City        = "Vancouver",
                    Province    = "BC",
                    PostalCode  = "V3U E2Y",
                    Country     = "Canada",
                    PhoneNumber = "6572136821"
                };

                var result = await userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    await userManager.AddPasswordAsync(user, password);

                    await userManager.AddToRoleAsync(user, role2);
                }
            }

            if (await userManager.FindByNameAsync("d") == null)
            {
                var user = new ApplicationUser
                {
                    UserName    = "******",
                    Email       = "[email protected]",
                    FirstName   = "Donald",
                    LastName    = "Duck",
                    Street      = "Well St",
                    City        = "Vancouver",
                    Province    = "BC",
                    PostalCode  = "V8U R9Y",
                    Country     = "Canada",
                    PhoneNumber = "6041234567"
                };

                var result = await userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    await userManager.AddPasswordAsync(user, password);

                    await userManager.AddToRoleAsync(user, role2);
                }
            }

            context.Boats.AddRange(DummyData.GetBoats(adminId1, adminId2));
            context.SaveChanges();

            context.Bookings.AddRange(DummyData.GetBookings(context));
            context.SaveChanges();
        }
Пример #2
0
        public static async Task Initialize(ApplicationDbContext context, RoleManager <IdentityRole> _roleManager, UserManager <ApplicationUser> _userManager)
        {
            context.Database.EnsureCreated();

            Debug.WriteLine("In Initializer");

            //Create admin role if not found.
            if (await _roleManager.FindByNameAsync("Admin") == null)
            {
                await _roleManager.CreateAsync(new IdentityRole("Admin"));
            }
            //Create full member - good standing role if not found.
            if (await _roleManager.FindByNameAsync("Good Standing Member") == null)
            {
                await _roleManager.CreateAsync(new IdentityRole("Good Standing Member"));
            }
            //Create full member - not good standing role if not found
            if (await _roleManager.FindByNameAsync("Not Good Standing Member") == null)
            {
                await _roleManager.CreateAsync(new IdentityRole("Not Good Standing Member"));
            }
            //Create associate member standing role if not found
            if (await _roleManager.FindByNameAsync("Associate Member") == null)
            {
                await _roleManager.CreateAsync(new IdentityRole("Associate Member"));
            }
            //Create social member standing role if not found
            if (await _roleManager.FindByNameAsync("Social Member") == null)
            {
                await _roleManager.CreateAsync(new IdentityRole("Social Member"));
            }
            //Create booking moderator standing role if not found
            if (await _roleManager.FindByNameAsync("Booking Moderator") == null)
            {
                await _roleManager.CreateAsync(new IdentityRole("Booking Moderator"));
            }

            //First member.
            if (await _userManager.FindByEmailAsync("[email protected]") == null)
            {
                var member1 = new ApplicationUser
                {
                    Email                 = "[email protected]",
                    UserName              = "******",
                    FirstName             = "Rob",
                    LastName              = "Stan",
                    MemberStatus          = "Full member good standing",
                    SkipperStatus         = "Crew",
                    Street                = "Cotton Drive",
                    City                  = "Vancouver",
                    Province              = "British Columbia",
                    PostalCode            = "V5N 1A7",
                    Country               = "Canada",
                    MobilePhone           = "7787770088",
                    HomePhone             = "",
                    WorkPhone             = "",
                    SailingQualifications = "None",
                    SailingExperience     = "None",
                    Credits               = 320,
                };
                var result = await _userManager.CreateAsync(member1, "P@$$w0rd");

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(member1, "Good Standing Member");
                }
            }

            if (await _userManager.FindByEmailAsync("[email protected]") == null)
            {
                var member1 = new ApplicationUser
                {
                    Email                 = "[email protected]",
                    UserName              = "******",
                    FirstName             = "Derek",
                    LastName              = "Livington",
                    MemberStatus          = "Full member good standing",
                    SkipperStatus         = "Day skipper",
                    Street                = "Fraser Street",
                    City                  = "Vancouver",
                    Province              = "British Columbia",
                    PostalCode            = "V1D 3B7",
                    Country               = "Canada",
                    MobilePhone           = "7781234657",
                    HomePhone             = "",
                    WorkPhone             = "",
                    SailingQualifications = "Sailing Legend",
                    SailingExperience     = "Born in the ocean",
                    Credits               = 320,
                };
                var result = await _userManager.CreateAsync(member1, "P@$$w0rd");

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(member1, "Admin");
                }
            }

            if (await _userManager.FindByEmailAsync("[email protected]") == null)
            {
                var member1 = new ApplicationUser
                {
                    Email                 = "[email protected]",
                    UserName              = "******",
                    FirstName             = "Ryan",
                    LastName              = "Smith",
                    MemberStatus          = "Full member not good standing",
                    SkipperStatus         = "Cruise skipper",
                    Street                = "East 11th Avenue",
                    City                  = "Vancouver",
                    Province              = "British Columbia",
                    PostalCode            = "V1D 3B7",
                    Country               = "Canada",
                    MobilePhone           = "6048976544",
                    HomePhone             = "",
                    WorkPhone             = "",
                    SailingQualifications = "None",
                    SailingExperience     = "None",
                    Credits               = 0,
                };
                var result = await _userManager.CreateAsync(member1, "P@$$w0rd");

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(member1, "Admin");
                }
            }

            // Seeding boats if there is no record
            if (!context.Boats.Any())
            {
                context.Boats.AddRange(DummyData.GetBoats());
            }

            if (!context.Bookings.Any())
            {
                context.Bookings.AddRange(DummyData.GetBookings(context));
            }

            if (!context.Reports.Any())
            {
                //TO-DO: Fix repots in dummy data first
                //context.Reports.AddRange(DummyData.GetReports());
                //await context.SaveChangesAsync();
            }

            context.SaveChanges();
        }