public bool AddSuperAdminSchool(long userId, long schoolInfoId) { try { Entity.SchoolUserMapping schoolUserMapping = new Entity.SchoolUserMapping { UserId = userId, SchoolInfoId = schoolInfoId }; _schoolContext.SchoolUserMapping.Add(schoolUserMapping); _schoolContext.SaveChanges(); return(true); } catch (Exception ex) { throw ex; } }
public Model.SchoolBasicInfo InsertUpdatedSchool(Model.SchoolBasicInfo schoolBasicInfo) { try { var config = new MapperConfiguration(cfg => { cfg.CreateMap <Model.SchoolBasicInfo, Entity.SchoolBasicInfo>(); }); IMapper mapper = config.CreateMapper(); var schoolInfo = mapper.Map <Model.SchoolBasicInfo, Entity.SchoolBasicInfo>(schoolBasicInfo); if (schoolInfo.SchoolInfoId == 0) { using (var dbContextTransaction = _schoolContext.Database.BeginTransaction()) { try { Entity.UserProfile userProfile = new Entity.UserProfile { FirstName = string.Empty, LastName = string.Empty, Email = schoolInfo.Email, Password = Utilities.CreateRandomPassword(8), ContactNumber = schoolInfo.ContactNumber, RoleId = (int)_schoolContext.Roles.Where(r => r.RoleName == "SchoolAdmin").Select(s => s.RoleID).FirstOrDefault(), UserId = 0 }; _schoolContext.UserProfile.Add(userProfile); _schoolContext.SaveChanges(); schoolInfo.IsActive = true; _schoolContext.SchoolBasicInfo.Add(schoolInfo); _schoolContext.SaveChanges(); if (userProfile.UserId > 0 && schoolInfo.SchoolInfoId > 0) { Entity.SchoolUserMapping schoolUserMapping = new Entity.SchoolUserMapping { SchoolInfoId = schoolInfo.SchoolInfoId, UserId = userProfile.UserId }; _schoolContext.SchoolUserMapping.Add(schoolUserMapping); } _schoolContext.SaveChanges(); schoolBasicInfo.SchoolInfoId = schoolInfo.SchoolInfoId; schoolBasicInfo.IsActive = schoolInfo.IsActive; schoolBasicInfo.UserId = userProfile.UserId; dbContextTransaction.Commit(); } catch (Exception ex) { dbContextTransaction.Rollback(); throw ex; } } } else { _schoolContext.Entry(schoolInfo).State = EntityState.Modified; _schoolContext.SaveChanges(); } return(schoolBasicInfo); } catch (Exception ex) { throw; } }