public async Task <UserEntity> GetOrAddUserAsync( string email, CancellationToken token) { using (var entityContext = _entityContextFactory.Create()) { var entities = await entityContext .Users .Where(x => x.Email.ToLower() == email.ToLower()) .ToListAsync(token); if (entities.Count > 1) { throw new InvalidOperationException("There duplicate users with this email address."); } var entity = entities.FirstOrDefault(); if (entity == null) { entity = new UserEntity { Email = email }; await entityContext.Users.AddAsync(entity, token); await entityContext.SaveChangesAsync(token); } return(entity); } }
/// <inheritdoc /> public virtual IUnitOfWorkContext BeginContext() { var entityContext = EntityContextFactory.Create(); return(new UnitOfWorkContext(entityContext, RepoProvider)); }