コード例 #1
0
        private async Task 建立訂單紀錄Async(Random random, List <Product> products)
        {
            for (int i = 0; i < 3; i++)
            {
                OrderMaster order = new OrderMaster()
                {
                    Name         = $"Order{i}",
                    OrderDate    = DateTime.Now.AddDays(random.Next(30)),
                    RequiredDate = DateTime.Now.AddDays(random.Next(30)),
                    ShippedDate  = DateTime.Now.AddDays(random.Next(30)),
                };
                context.Add(order);
                await context.SaveChangesAsync();

                var total = random.Next(1, 6);
                for (int j = 0; j < total; j++)
                {
                    OrderItem orderItem = new OrderItem()
                    {
                        Name          = $"OrderItem{j}",
                        OrderMasterId = order.Id,
                        ProductId     = products[j].Id,
                        Quantity      = 3,
                        ListPrice     = 168,
                    };
                    context.Add(orderItem);
                }
                await context.SaveChangesAsync();
            }
            CleanTrackingHelper.Clean <Product>(context);
            CleanTrackingHelper.Clean <OrderMaster>(context);
            CleanTrackingHelper.Clean <OrderItem>(context);
        }
コード例 #2
0
        private async Task 建立使用者紀錄Async(string InitializationMode)
        {
            #region 建立使用者紀錄

            CleanTrackingHelper.Clean <MyUser>(context);
            #region 取得各種需要的角色
            var menuRole開發者 = await context.MenuRole
                              .AsNoTracking()
                              .FirstOrDefaultAsync(x => x.Name == MagicHelper.開發者功能表角色);

            var menuRole系統管理員 = await context.MenuRole
                                .AsNoTracking()
                                .FirstOrDefaultAsync(x => x.Name == MagicHelper.系統管理員功能表角色);

            var menuRole使用者 = await context.MenuRole
                              .AsNoTracking()
                              .FirstOrDefaultAsync(x => x.Name == MagicHelper.使用者功能表角色);

            #endregion

            #region 建立 開發者
            var myUser = new MyUser()
            {
                Account                     = $"god",
                Name                        = $"開發者",
                MenuRoleId                  = menuRole開發者.Id,
                Status                      = true,
                Salt                        = Guid.NewGuid().ToString(),
                ForceLogoutDatetime         = DateTime.Now.AddDays(-1),
                ForceChangePassword         = false,
                ForceChangePasswordDatetime = DateTime.Now.AddDays(42),
                LoginFailTimes              = 0,
                LoginFailUnlockDatetime     = DateTime.Now.AddDays(-1),
                LastLoginDatetime           = DateTime.Now,
                Email                       = "*****@*****.**",
            };

            myUser.Salt     = Guid.NewGuid().ToString();
            myUser.Password =
                PasswordHelper.GetPasswordSHA(myUser.Salt + "Vulcan", "abc");

            context.Add(myUser);
            await context.SaveChangesAsync();

            CleanTrackingHelper.Clean <MyUser>(context);
            #endregion

            #region 建立 系統管理員
            var adminMyUser = new MyUser()
            {
                Account                     = $"{MagicHelper.系統管理員帳號}",
                Name                        = $"系統管理員 {MagicHelper.系統管理員帳號}",
                MenuRoleId                  = menuRole系統管理員.Id,
                Status                      = true,
                Salt                        = Guid.NewGuid().ToString(),
                ForceLogoutDatetime         = DateTime.Now.AddDays(-1),
                ForceChangePassword         = false,
                ForceChangePasswordDatetime = DateTime.Now.AddDays(42),
                LoginFailTimes              = 0,
                LoginFailUnlockDatetime     = DateTime.Now.AddDays(-1),
                LastLoginDatetime           = DateTime.Now,
                Email                       = "*****@*****.**",
            };
            var adminRawPassword = "******";
            adminMyUser.Password =
                PasswordHelper.GetPasswordSHA(adminMyUser.Salt, adminRawPassword);

            context.Add(adminMyUser);
            await context.SaveChangesAsync();

            CleanTrackingHelper.Clean <MyUser>(context);
            #endregion

            if (InitializationMode == "開發模式")
            {
                #region 建立 使用者
                foreach (var item in MagicHelper.使用者帳號)
                {
                    var itemMyUser = await context.MyUser
                                     .AsNoTracking()
                                     .FirstOrDefaultAsync(x => x.Name == item);

                    if (itemMyUser == null)
                    {
                        itemMyUser = new MyUser()
                        {
                            Account                     = $"{item}",
                            Name                        = $"使用者 {item}",
                            MenuRoleId                  = menuRole使用者.Id,
                            Status                      = true,
                            Salt                        = Guid.NewGuid().ToString(),
                            ForceLogoutDatetime         = DateTime.Now.AddDays(-1),
                            ForceChangePassword         = false,
                            ForceChangePasswordDatetime = DateTime.Now.AddDays(42),
                            LoginFailTimes              = 0,
                            LoginFailUnlockDatetime     = DateTime.Now.AddDays(-1),
                            LastLoginDatetime           = DateTime.Now,
                            Email                       = "*****@*****.**",
                        };
                        var userRawPassword = "******";
                        itemMyUser.Password =
                            PasswordHelper.GetPasswordSHA(itemMyUser.Salt, userRawPassword);

                        context.Add(itemMyUser);
                        await context.SaveChangesAsync();

                        CleanTrackingHelper.Clean <MyUser>(context);
                    }
                }
                #endregion
            }
            #endregion
        }
コード例 #3
0
        public async Task InitDataAsync()
        {
            Random random = new Random();

            #region 適用於 Code First ,刪除資料庫與移除資料庫
            await context.Database.EnsureDeletedAsync();

            await context.Database.EnsureCreatedAsync();

            #endregion

            #region 建立使用者紀錄
            var items = Get姓名();
            CleanTrackingHelper.Clean <MyUser>(context);
            int idx = 1;
            foreach (var item in items)
            {
                var itemMyUser = await context.MyUser
                                 .AsNoTracking()
                                 .FirstOrDefaultAsync(x => x.Name == item);

                if (itemMyUser == null)
                {
                    itemMyUser = new MyUser()
                    {
                        Account  = $"user{idx}",
                        Name     = $"{item}",
                        Password = "******",
                    };
                    if (idx == 9)
                    {
                        itemMyUser.IsManager = true;
                    }
                    context.Add(itemMyUser);
                    await context.SaveChangesAsync();

                    idx++;
                }
            }
            CleanTrackingHelper.Clean <MyUser>(context);
            #endregion

            #region 建立產品紀錄
            CleanTrackingHelper.Clean <Product>(context);
            CleanTrackingHelper.Clean <Order>(context);
            CleanTrackingHelper.Clean <OrderItem>(context);
            List <Product> products = new List <Product>();
            for (int i = 0; i < 10; i++)
            {
                Product product = new Product()
                {
                    Name = $"Product{i}"
                };
                products.Add(product);
                context.Add(product);
            }
            await context.SaveChangesAsync();

            #endregion

            #region 建立產品紀錄
            for (int i = 0; i < 3; i++)
            {
                Order order = new Order()
                {
                    Name         = $"Order{i}",
                    OrderDate    = DateTime.Now.AddDays(random.Next(30)),
                    RequiredDate = DateTime.Now.AddDays(random.Next(30)),
                    ShippedDate  = DateTime.Now.AddDays(random.Next(30)),
                };
                context.Add(order);
                await context.SaveChangesAsync();

                var total = random.Next(1, 6);
                for (int j = 0; j < total; j++)
                {
                    OrderItem orderItem = new OrderItem()
                    {
                        Name      = $"OrderItem{j}",
                        OrderId   = order.Id,
                        ProductId = products[j].Id,
                        Quantity  = 3,
                        ListPrice = 168,
                    };
                    context.Add(orderItem);
                }
                await context.SaveChangesAsync();
            }
            CleanTrackingHelper.Clean <Product>(context);
            CleanTrackingHelper.Clean <Order>(context);
            CleanTrackingHelper.Clean <OrderItem>(context);
            #endregion
        }