private static async Task <ConscienceAccount> CreateOrUpdateAccount(IUsersIdentityService identityService, ConscienceContext context, string name, string password, string role) { var account = context.Accounts.FirstOrDefault(a => a.UserName.ToLower() == name.ToLower()); if (account == null) { account = new ConscienceAccount { UserName = name, PasswordHash = "AA+JyvawjlityY0fyaRSr1qZkCgaIvU+bqTe6uShANUqcoG0EH2F6BcXq7hwnN7N1Q==" }; var roleName = Enum.GetNames(typeof(RoleTypes)).First(r => r.ToLowerInvariant() == role.ToLowerInvariant()); account.Roles.Add(new Role { Name = roleName }); context.Accounts.Add(account); context.SaveChanges(); } account.PictureUrl = "/Content/images/uploaded/" + account.Id + ".jpg?_ts=101"; context.SaveChanges(); await identityService.ChangePasswordAsync(account.Id, password); return(account); }
private static void AddCharacterInPlot(ConscienceContext context, Dictionary <string, Plot> plots, IRow row, Character character, int plotColumn) { var plotCode = row.GetCell(plotColumn).ToCleanString().Trim(); if (string.IsNullOrWhiteSpace(plotCode)) { return; } var plotFunction = row.GetCell(plotColumn + 3).ToCleanString().Trim(); var hasEvent = row.GetCell(plotColumn + 4).ToCleanString().Trim().ToLowerInvariant() == "yes"; if (!plots.ContainsKey(plotCode)) { throw new Exception("There is no plot with code " + plotCode); } var plot = plots[plotCode]; character.Plots.Add(new CharacterInPlot { Description = plotFunction, Plot = plot }); if (hasEvent) { plot.Events.First().Characters.Add(character); } context.SaveChanges(); }
private void ClearSet <T>(ConscienceContext context, DbSet <T> set, Expression <Func <T, bool> > excludePredicate = null) where T : class { List <T> values = null; if (excludePredicate != null) { values = set.Where(excludePredicate).ToList(); } else { values = set.ToList(); } foreach (var value in values) { set.Remove(value); } context.SaveChanges(); }
private void ClearDb(ConscienceContext context, ConscienceAccount currentUser) { var roles = currentUser.Roles.ToList(); #if DEBUG context.Database.Delete(); context.Database.Create(); var account = new ConscienceAccount { UserName = currentUser.UserName, PasswordHash = currentUser.PasswordHash }; context.Accounts.Add(account); foreach (var role in roles) { account.Roles.Add(new Role { Name = role.Name }); } context.SaveChanges(); #endif }
private static void AddRelation(ConscienceContext context, BulkImportResult result, Dictionary <int, Character> characters, IRow row, Character character, string hostName, int relationColumn) { if (row.GetCell(relationColumn) == null) { return; } var relation = row.GetCell(relationColumn).ToCleanString().Trim(); if (string.IsNullOrWhiteSpace(relation)) { return; } var reader = new StringReader(relation); var name = reader.ReadLine().Trim(); var description = reader.ReadToEnd(); var relatedCharacter = characters.Values.FirstOrDefault(c => c.Name.ToLowerInvariant() == name.ToLowerInvariant()); if (relatedCharacter == null) { result.Errors.Add(new BulkImportError { Line = "Characrer relations - " + hostName, Error = "There is no character with name " + name }); return; } character.Relations.Add(new CharacterRelation { Character = relatedCharacter, Description = description }); context.SaveChanges(); }
public T Add(T entity) { DbSet.Add(entity); _context.SaveChanges(); return(entity); }