/// <summary> /// Check if the DB has at least one user. /// </summary> /// <returns></returns> public bool DbHasUsers() { using (var context = new DomainDBContext()) { return(context.Users.Any()); } }
/// <summary> /// Get user login guid using login credential /// </summary> /// <param name="email">Email</param> /// <param name="password">Password</param> /// <returns>string userGuid</returns> public string GetUserLoginGuidWithLoginCredentials(string email, string password) { using (var context = new DomainDBContext()) { return(context.Users .Where(u => u.Email.ToLower() == email.ToLower() && u.Password.ToLower() == password.ToLower()) .Select(s => s.UserGuid) .FirstOrDefault()); } }
public string GetUserRoleName(string userGuid) { using (var context = new DomainDBContext()) { return(context.Users .Include(u => u.Role) .Where(u => u.UserGuid == userGuid) .Select(u => u.Role.Name.ToLower()) .FirstOrDefault()); } }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { using (var dbContext = new DomainDBContext()) { var roleExist = dbContext.Roles.Any(r => r.Name.ToLower() == Name.ToLower()); if (roleExist) { yield return(new ValidationResult("This role already exist, please enter a new name.", new string[] { "Name" })); } } }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { using (var dbContext = new DomainDBContext()) { var userExist = dbContext.Users.Any(u => u.Email.ToLower() == Email.ToLower()); if (userExist) { yield return(new ValidationResult("The entered email already exist, please enter a new email.", new[] { "Email" })); } } }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { using (var dbContext = new DomainDBContext()) { var categoryExist = dbContext.Categories.Any(c => c.Name.ToLower() == Name.ToLower()); if (categoryExist) { yield return(new ValidationResult("This category already exist, please enter a new name.", new string [] { "Name" })); } } }
public IList <BlogSummary> GetBlogs(int pageNo, int pageSize) { using (var context = new DomainDBContext()) { return(context.Blogs .OrderBy(b => b.Id) .Skip((pageNo - 1) * pageSize).Take(pageSize) .Select(b => new BlogSummary() { Title = b.Title, AuthorName = b.Author.Name, NumberOfComments = b.Comments.Count(), DatePosted = b.CreatedDate, BlogId = b.Id, NumberOfViews = 0 }) .ToList()); } }
public bool CreateAdminUser(User user, Author author, Role role, out List <string> errorList) { errorList = new List <string>(); var saveSuccessful = true; using (var context = new DomainDBContext()) { try { LogQueryToVSTrace(context); AutoDetectChange(context, false); context.Roles.Add(role); context.Users.Add(user); context.Authors.Add(author); context.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (var entityValidationError in context.GetValidationErrors()) { foreach (var error in entityValidationError.ValidationErrors) { errorList.Add("Entity: " + entityValidationError.Entry.Entity.GetType().FullName + " Property Name: " + error.PropertyName + " ErrorMessage: " + error.ErrorMessage); } } saveSuccessful = false; } finally { AutoDetectChange(context, true); } } return(saveSuccessful); }
/// <summary> /// Disable Auto detect changes to aid multiple insert performance or not. /// </summary> /// <param name="context">DbContext instance</param> /// <param name="detect">Disable or Enable AutoDetectChanges during record insert</param> public void AutoDetectChange(DomainDBContext context, bool detect) { context.Configuration.AutoDetectChangesEnabled = detect; }
/// <summary> /// Save changes to DB /// </summary> /// <returns>Action status</returns> public bool SaveChanges(DomainDBContext context) { context.SaveChanges(); return(true); }
public void LogQueryToVSTrace(DomainDBContext context) { context.Database.Log = message => Trace.WriteLine(message); }