コード例 #1
0
        /// <summary>
        /// 100データを作成します
        /// </summary>
        public async Task <IActionResult> Create100Data()
        {
            UserModel usermodel = new UserModel();

            Random ran        = new Random();
            int    rankPoints = 0;
            String letters    = "abcdefghijklmnopqrstuvwxyz";
            int    length     = 8;
            int    maxid      = 0;
            String randomName = "";

            for (int j = 0; j < 100; j++)
            {
                rankPoints = ran.Next(100);
                randomName = "";
                var rec = _context.Users.FirstOrDefault();

                if (rec == null)
                {
                    maxid = 0;
                }
                else
                {
                    maxid = _context.Users.Max(p => p.UserId);
                }
                for (int i = 0; i < length; i++)
                {
                    int c = ran.Next(26);
                    randomName = randomName + letters.ElementAt(c);
                }

                usermodel.UserId     = maxid + 1;
                usermodel.RankPoints = rankPoints;
                usermodel.UserName   = randomName;

                _context.Add(usermodel);
                _context.Database.OpenConnection();
                try
                {
                    _context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Users ON");
                    _context.SaveChanges();
                    _context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Users OFF");
                }
                finally
                {
                    _context.Database.CloseConnection();
                }
            }
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
コード例 #2
0
        // [Route("api/User")]
        public IActionResult Create100Data()
        {
            UserModel usermodel = new UserModel();

            Random ran        = new Random();
            int    rankPoints = 0;
            String letters    = "abcdefghijklmnopqrstuvwxyz";
            int    length     = 8;
            int    maxid      = 0;
            String randomName = "";

            try
            {
                for (int j = 0; j < 100; j++)
                {
                    rankPoints = ran.Next(100);
                    randomName = "";
                    var rec = _context.Users.FirstOrDefault();

                    if (rec == null)
                    {
                        maxid = 0;
                    }
                    else
                    {
                        maxid = _context.Users.Max(p => p.UserId);
                    }
                    for (int i = 0; i < length; i++)
                    {
                        int c = ran.Next(26);
                        randomName = randomName + letters.ElementAt(c);
                    }

                    usermodel.UserId     = maxid + 1;
                    usermodel.RankPoints = rankPoints;
                    usermodel.UserName   = randomName;

                    _context.Add(usermodel);
                    _context.Database.OpenConnection();
                    // _context.Database.OpenConnection();
                    try
                    {
                        _context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Users ON");
                        _context.SaveChanges();
                        _context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Users OFF");
                    }
                    finally
                    {
                        _context.Database.CloseConnection();
                    }
                }
                _context.SaveChangesAsync();
                return(Ok(new { success = true, message = "User Inserted" }));
            }
            catch (Exception ex)
            {
                return(Ok(new { success = false, message = ex.Message }));
            }
        }