コード例 #1
0
 /// <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());
     }
 }
コード例 #2
0
 /// <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());
     }
 }
コード例 #3
0
 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());
     }
 }
コード例 #4
0
ファイル: Role.cs プロジェクト: pophils/EF6ReinforcerApp
        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" }));
                }
            }
        }
コード例 #5
0
ファイル: User.cs プロジェクト: pophils/EF6ReinforcerApp
        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" }));
                }
            }
        }
コード例 #6
0
ファイル: Category.cs プロジェクト: pophils/EF6ReinforcerApp
        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" }));
                }
            }
        }
コード例 #7
0
 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());
     }
 }
コード例 #8
0
        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);
        }
コード例 #9
0
 /// <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;
 }
コード例 #10
0
 /// <summary>
 /// Save changes to DB
 /// </summary>
 /// <returns>Action status</returns>
 public bool SaveChanges(DomainDBContext context)
 {
     context.SaveChanges();
     return(true);
 }
コード例 #11
0
 public void LogQueryToVSTrace(DomainDBContext context)
 {
     context.Database.Log = message => Trace.WriteLine(message);
 }