Example #1
0
        /// <summary>
        /// Initializes the specified service provider.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new WebApiDBContext(
                       serviceProvider.GetRequiredService <DbContextOptions <WebApiDBContext> >()))
            {
                if (context.UsersInfo.Any())
                {
                    return;  // Data was already seeded
                }

                context.UsersInfo.AddRange(
                    new User
                {
                    Id          = "1",
                    FirstName   = "Steve",
                    LastName    = "Finn",
                    Country     = "UK",
                    Employer    = "Facebook",
                    Designation = "Senior Engineer"
                },
                    new User
                {
                    Id          = "2",
                    FirstName   = "Jack",
                    LastName    = "Will",
                    Country     = "Australia",
                    Employer    = "Google",
                    Designation = "Consultant"
                },
                    new User
                {
                    Id          = "3",
                    FirstName   = "Viraj",
                    LastName    = "Sharma",
                    Country     = "India",
                    Employer    = "Microsoft",
                    Designation = "Consultant"
                },
                    new User
                {
                    Id          = "4",
                    FirstName   = "Thomas",
                    LastName    = "Miller",
                    Country     = "US",
                    Employer    = "Walmart",
                    Designation = "Program Manager"
                },
                    new User
                {
                    Id          = "5",
                    FirstName   = "Jack",
                    LastName    = "Ryan",
                    Country     = "US",
                    Employer    = "AT&T",
                    Designation = "Senior Consultant"
                });
                context.SaveChanges();
            }
        }
Example #2
0
        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="users">The users.</param>
        /// <returns>
        /// Response
        /// </returns>
        public bool UpdateUser(List <User> users)
        {
            try
            {
                foreach (var user in users)
                {
                    var selectedUser = _dbContext.UsersInfo.Where(x => x.Id == user.Id).FirstOrDefault();
                    selectedUser.FirstName   = user.FirstName;
                    selectedUser.LastName    = user.LastName;
                    selectedUser.Employer    = user.Employer;
                    selectedUser.Designation = user.Designation;
                    selectedUser.Country     = user.Country;

                    _dbContext.SaveChanges();
                }
                return(true);
            }
            catch
            {
                //Log Exception
                return(false);
            }
        }