public async Task <Guid> Create(API.User user, string password, string[] groups, CancellationToken cancellation) { if (await Connection.ExistsAsync <DAL.Login>(l => l.EmailOrUserName == user.EmailOrUserName && l.DeletedUtc == null, cancellation)) { throw new InvalidOperationException(Resources.USER_ALREADY_EXISTS); } Guid[] groupIds = (await Connection.SelectAsync <DAL.Group>(grp => Sql.In(grp.Name, groups.Distinct()), cancellation)) .Select(grp => grp.Id) .ToArray(); if (groupIds.Length != groups.Length) { throw new InvalidOperationException(Resources.INVALID_GROUP); } using (IBulkedDbConnection bulk = Connection.CreateBulkedDbConnection()) { var loginEntry = new DAL.Login { EmailOrUserName = user.EmailOrUserName, PasswordHash = HashPassword(password, GenerateSalt()) }; bulk.Insert(loginEntry); Debug.Assert(loginEntry.Id != Guid.Empty); var userEntry = new DAL.User { LoginId = loginEntry.Id, FullName = user.FullName }; bulk.Insert(userEntry); Debug.Assert(userEntry.Id != Guid.Empty); // // Don't use InsertAll() since it gives the same Id for each entry // foreach (Guid groupId in groupIds) { var ug = new DAL.UserGroup { GroupId = groupId, UserId = userEntry.Id }; bulk.Insert(ug); Debug.Assert(ug.Id != Guid.Empty); } await bulk.FlushAsync(cancellation); return(userEntry.Id); } }