public string Register(DbEntities.User.Users user) { try { var xUser = Context.Users.FirstOrDefault(x => x.UserName == user.UserName); if (xUser == null) { using (var scope = new TransactionScope()) { var savedUser = Context.Users.Add(user); Context.SaveChanges(); var roleId = Context.Role.FirstOrDefault(x => x.RoleName == "manager"); if (roleId == null) { roleId = Context.Role.Add(new Role() { DisplayName = "Manager" , RoleName = "manager" , Description = "'Manager' has complete access over all of the settings." }); Context.SaveChanges(); } var urole = new UserRole() { AssignedDate = DateTime.Now , UserId = savedUser.Id , RoleId = roleId.Id }; Context.UserRole.Add(urole); Context.SaveChanges(); scope.Complete(); return("success"); } } return("Username already exist."); } catch (Exception e) { return("Error while creating user."); } }
//used public DbEntities.User.Users AddOrUpdateStudent(DbEntities.User.Users user, DbEntities.Students.Student student, int programBatchId = 0) { try { using (var scope = new TransactionScope()) { //byte[] imgBytes = null; //using (var filehelper = new DbHelper.WorkingWithFiles()) //{ // //if (file != null) // //{ // // //imgBytes = filehelper.ConvertToBytes(file); // // //below comented due to changed in database.. now image is saved as file. // // //user.Image = imgBytes; // // //user.ImageType = file.ContentType; // //} //} var prev = Context.Users.Find(user.Id); if (prev == null) { user.FirstName = user.FirstName.Trim(); user.LastName = user.LastName.Trim(); prev = Context.Users.Add(user); Context.SaveChanges(); if (prev != null) { student.UserId = prev.Id; student.Name = prev.FullName; var std = Context.Student.Add(student); Context.SaveChanges(); if (std != null) { var role = Context.Role.FirstOrDefault(x => x.RoleName == "student"); if (role != null) { var userRole = new UserRole() { AssignedDate = DateTime.Now , RoleId = role.Id , UserId = prev.Id }; Context.UserRole.Add(userRole); Context.SaveChanges(); } if (programBatchId > 0) { var stdBatch = new DbEntities.Batches.StudentBatch() { ProgramBatchId = programBatchId , StudentId = std.Id , AddedDate = DateTime.Now }; Context.StudentBatch.Add(stdBatch); Context.SaveChanges(); } } } } else { prev.CreatedDate = user.CreatedDate; prev.DOB = user.DOB; prev.FirstName = user.FirstName; //below two lines commnented due to changes in database: now image saved as file. //prev.Image = user.Image; //prev.ImageType = user.ImageType; //prev.InstitutionId = createdUser.InstitutionId; prev.SchoolId = user.SchoolId; //prev.BarcodeNo = createdUser.BarcodeNo; //prev.Citizenship = createdUser.Citizenship; prev.City = user.City; prev.Country = user.Country; prev.Email = user.Email; prev.UserName = user.UserName; //prev.Street = createdUser.Street; //prev.UserTypeId = createdUser.UserTypeId; //prev.Religion = createdUser.Religion; //prev.Phone2 = createdUser.Phone2; //prev.Phone1 = createdUser.Phone1; prev.Password = user.Password; //prev.Nationality = createdUser.Nationality; //prev.MiddleName = createdUser.MiddleName; prev.LastName = user.LastName; prev.IsActive = user.IsActive; prev.IsDeleted = user.IsDeleted; //prev.Religion = createdUser.Religion; var std = Context.Student.Find(student.Id); if (std == null) { student.UserId = prev.Id; Context.Student.Add(student); } Context.SaveChanges(); } scope.Complete(); return(prev); } } catch (Exception) { return(null); } }
public void Seed() { var gender = _context.Gender.First(x => x.Name == "Rather not say"); var school = _context.School.First(x => x.Code == SchoolSeeder.NEC_CODE); var adminRole = _context.Role.First(x => x.RoleName == RoleSeeder.ADMIN_ROLE_NAME); var managerRole = _context.Role.First(x => x.RoleName == RoleSeeder.MANAGER_ROLE_NAME); var teacherRole = _context.Role.First(x => x.RoleName == RoleSeeder.TEACHER_ROLE_NAME); var studentRole = _context.Role.First(x => x.RoleName == RoleSeeder.STUDENT_ROLE_NAME); // var school = _context.School.FirstOrDefault(x => x.Code == SchoolSeeder.NEC_CODE); var users = new DbEntities.User.Users[] { new DbEntities.User.Users() { UserName = "******", Password = "******", FirstName = "Manager", MiddleName = "", LastName = "", SchoolId = school?.Id,//null, GenderId = gender.Id, Email = "", DOB = null, IsActive = true, IsDeleted = false, Description = "", CreatedDate = DateTime.Now, Country = "Nepal", EmailDisplay = "", Phone = "", City = "", SecurityQuestion = "", SecurityAnswer = "", UserRoles = new List <DbEntities.User.UserRole> { new DbEntities.User.UserRole { AssignedDate = DateTime.Now, RoleId = managerRole.Id }, new DbEntities.User.UserRole { AssignedDate = DateTime.Now, RoleId = adminRole.Id }, }, UserImageId = null, LastOnline = null, }, new DbEntities.User.Users() { UserName = "******", Password = "******", FirstName = "Teacher", MiddleName = "", LastName = "", SchoolId = school?.Id,//null, GenderId = gender.Id, Email = "", DOB = null, IsActive = true, IsDeleted = false, Description = "", CreatedDate = DateTime.Now, Country = "Nepal", EmailDisplay = "", Phone = "", City = "", SecurityQuestion = "", SecurityAnswer = "", UserRoles = new List <DbEntities.User.UserRole> { new DbEntities.User.UserRole { AssignedDate = DateTime.Now, RoleId = teacherRole.Id }, }, UserImageId = null, LastOnline = null, }, new DbEntities.User.Users() { UserName = "******", Password = "******", FirstName = "Student", MiddleName = "", LastName = "", SchoolId = school?.Id,//null, GenderId = gender.Id, Email = "", DOB = null, IsActive = true, IsDeleted = false, Description = "", CreatedDate = DateTime.Now, Country = "Nepal", EmailDisplay = "", Phone = "", City = "", SecurityQuestion = "", SecurityAnswer = "", UserRoles = new List <DbEntities.User.UserRole> { new DbEntities.User.UserRole { AssignedDate = DateTime.Now, RoleId = studentRole.Id }, }, UserImageId = null, LastOnline = null, }, }; _context.Users.AddOrUpdate( p => p.UserName, users ); _context.SaveChanges(); }
//User public DbEntities.User.Users AddOrUpdateUser(DbEntities.User.Users user, string roleId, UserFile userFile) { try { var date = DateTime.Now.Date; using (TransactionScope scope = new TransactionScope()) { //byte[] imgBytes = null; //using (var filehelper = new DbHelper.WorkingWithFiles()) //{ // if (file != null) // { // imgBytes = filehelper.ConvertToBytes(file); // user.Image = imgBytes; // user.ImageType = file.ContentType; // } //} var ent = Context.Users.Find(user.Id); if (ent == null) { if (userFile != null) { var img = Context.File.Add(userFile); Context.SaveChanges(); user.UserImageId = img.Id; } ent = Context.Users.Add(user); Context.SaveChanges(); if (roleId != "") { int rolei = Convert.ToInt32(roleId); if (rolei > 0) { SaveUsersRole(new List <int>() { ent.Id }, rolei, new List <int>()); //var role = new UserRole() //{ // RoleId = rolei, // UserId = entity.Id // , // AssignedDate = DateTime.Now.Date //}; //Context.UserRole.Add(role); //Context.SaveChanges(); } } } else { if (ent.UserImageId <= 0) { if (userFile != null) { var img = Context.File.Add(userFile); Context.SaveChanges(); ent.UserImageId = img.Id; } //school.ImageId = img.Id; //ent.ImageId = img.Id; //Context.Dispose(); } else { var img = Context.File.Find(ent.UserImageId); if (img != null && userFile != null) { img.DisplayName = userFile.DisplayName; img.FileName = userFile.FileName; img.ModifiedBy = userFile.ModifiedBy; img.ModifiedDate = userFile.ModifiedDate; img.FileDirectory = userFile.FileDirectory; img.FileSizeInBytes = userFile.FileSizeInBytes; img.FileType = userFile.FileType; img.IconPath = userFile.IconPath; img.Void = userFile.Void; //Context.SaveChanges(); } } ent.City = user.City; ent.Country = user.Country; ent.DOB = user.DOB; ent.Description = user.Description; ent.Email = user.Email; ent.FirstName = user.FirstName; ent.MiddleName = user.MiddleName; ent.EmailDisplay = user.EmailDisplay; ent.Email = user.Email; ent.UserName = user.UserName; ent.Phone = user.Phone; ent.IsDeleted = user.IsDeleted; ent.IsActive = user.IsActive; ent.Password = user.Password; ent.LastName = user.LastName; ent.GenderId = user.GenderId; Context.SaveChanges(); } scope.Complete(); return(ent); } } catch (Exception) { return(null); } }