public NhibernateDatabaseContext() { SessionFactory = CreateSessionFactory(); using (ISession session = SessionFactory.OpenSession()) using (var transaction = session.BeginTransaction()) { var groupRepository = new Repository<Group>(session); var group = groupRepository.FindOne(x => x.Name == _groupName); if (group == null) { group = new Group { Name = _groupName, IsSysAccount = true, }; var user = new User { FirstName = "System", LastName = "Administrator", Username = "******", Nickname = "SuperAdmin", Password = "******", Group = group, }; groupRepository.Add(group); (new Repository<User>(session)).Add(user); transaction.Commit(); } } }
public MessageResultDto AddUser(string firstname, string lastname, string nickname, string username, Guid groupId) { User user; var result = new MessageResultDto(); using (ISession session = _sessionFactory.OpenSession()) using (ITransaction transaction = session.BeginTransaction()) { IRepository<Group> groupRepository = new Repository<Group>(session); var group = groupRepository.FindOne(x => x.Id == groupId); IRepository<User> userRepository = new Repository<User>(session); var existingUser = userRepository.FindOne(new Tuple<Expression<Func<User, object>>, string>(x => x.Username, username)); if (null == existingUser) { user = new User { FirstName = firstname, LastName = lastname, Nickname = nickname, Username = username, Password = Constants.DEFAULT_PASSWORD, Group = group }; userRepository.Add(user); transaction.Commit(); result.SuccessMessage("User added successfully", user.Id); } else { result.ErrorMessage("User with same username already exists"); } } return result; }
public Account() { User = new User(); Transactions = new List<Transaction>(); }