예제 #1
0
        public void Seed()
        {
            if (_dbContext.Database.CanConnect())
            {
                var pendingMigrations = _dbContext.Database.GetPendingMigrations();

                if (pendingMigrations != null && pendingMigrations.Any())
                {
                    _dbContext.Database.Migrate();
                }

                if (!_dbContext.Roles.Any())
                {
                    var roles = GetRoles();
                    _dbContext.AddRange(roles);
                    _dbContext.SaveChanges();
                }

                if (!_dbContext.Locations.Any())
                {
                    var locations = GetLocations();
                    _dbContext.AddRange(locations);
                    _dbContext.SaveChanges();
                }
            }
        }
예제 #2
0
        public User AddUser(User user)
        {
            context.User.Add(user);
            context.SaveChanges();


            return(user);
        }
예제 #3
0
        public static void Initialize(IServiceProvider services)
        {
            CCDbContext context = services.GetRequiredService <CCDbContext>();

            context.Database.EnsureCreated();


            if (!context.Messages.Any())
            {
                User use = new User {
                    Name = "Test User1", EmailAddress = "*****@*****.**"
                };
                context.User.Add(use);
                context.SaveChanges();
                Message mess = new Message {
                    ContactMessage = "Test Message Y", UserID = use.UserID
                };
                context.Messages.Add(mess);
                use.Messages.Add(mess);

                use = new User {
                    Name = "Test User2", EmailAddress = "*****@*****.**"
                };
                context.User.Add(use);
                context.SaveChanges();
                mess = new Message {
                    ContactMessage = "Test Message X", UserID = use.UserID
                };
                context.Messages.Add(mess);
                use.Messages.Add(mess);

                use = new User {
                    Name = "Test User3", EmailAddress = "*****@*****.**"
                };
                context.User.Add(use);
                context.SaveChanges();
                mess = new Message {
                    ContactMessage = "Test Message Z", UserID = use.UserID
                };
                context.Messages.Add(mess);
                use.Messages.Add(mess);
                context.SaveChanges();
            }
        }
예제 #4
0
        public void RegisterUser(RegisterUserDto dto)
        {
            var newUser = new User()
            {
                FirstName = dto.FirstName,
                Email     = dto.Email,
                LastName  = dto.LastName,
                RoleId    = dto.RoleId
            };

            var hashedPassword = _passwordHasher.HashPassword(newUser, dto.Password);

            newUser.PasswordHash = hashedPassword;

            _dbContext.Add(newUser);
            _dbContext.SaveChanges();
        }
 public User AddUser(User user)
 {
     if (user == null)
     {
         throw new System.Exception("user参数为空");
     }
     try
     {
         var existing = _db.Users.Where(
             pc => pc.UserName == user.UserName).SingleOrDefault();
         if (existing == null)
         {
             AddFullAuditDefault <User>(user);
             _db.Users.Add(user);
             _db.SaveChanges();
         }
         return(user);
     }
     catch
     {
         return(null);
     }
 }
예제 #6
0
 public int Update(User user)
 {
     context.Users.Update(user);
     return(context.SaveChanges());
 }