public async Task <User> Register(User user, string password) { byte[] passwordHash, passwordSalt; CreatePasswordHash(password, out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; await _dbContext.AddAsync(user); await _dbContext.SaveChangesAsync(); return(user); }
public async Task CreatesNpgSqlComposite() { var parent = new Efcoretest { Name = $"{nameof(BulkAddTests)}_Composite", Createddate = DateTime.UtcNow, Modifieddate = DateTime.UtcNow }; using (var db = new postgresContext()) { await db.AddAsync(parent); await db.SaveChangesAsync(); } var toCreate = new List <Efcoretestchild>(); for (var i = 0; i < 10; i++) { toCreate.Add(new Efcoretestchild { Efcoretestid = parent.Id, Name = i == 0 ? null : $"{nameof(BulkAddTests)}_{i}", Createddate = DateTime.UtcNow, Modifieddate = DateTime.UtcNow }); } using (var db = new postgresContext()) { await db.Efcoretestchild.BulkAddAsync(toCreate); foreach (var e in toCreate) { var added = await db.Efcoretestchild.FindAsync(e.Id, e.Efcoretestid); Assert.NotNull(added); Assert.Equal(e.Name, added.Name); } } }
public async Task MergesNpgSql(BulkMergeNotMatchedBehavior notMatchedBehavior) { List <Efcoretest> entities; var dontDelete = new Efcoretest { Name = "dont delete", Createddate = DateTime.UtcNow, Modifieddate = DateTime.UtcNow }; using (var db = new postgresContext()) { entities = await db.Efcoretest .Where(e => e.Name.Contains(nameof(BulkMergeTests))) .ToListAsync(); var added = await db.AddAsync(dontDelete); await db.SaveChangesAsync(); dontDelete.Id = added.Entity.Id; } const int numUpdate = 5; var toMerge = entities .OrderBy(e => e.Id) .Take(numUpdate) .Select(x => { x.Modifieddate = DateTime.UtcNow.AddDays(x.Id); return(x); }) .ToList(); var numNotMatched = entities.Count - toMerge.Count; var numAdd = 2; for (var i = 0; i < numAdd; i++) { toMerge.Add(new Efcoretest { Name = $"{nameof(BulkMergeTests)}_insert_{i}", Modifieddate = DateTime.Now, Createddate = DateTime.Now }); } var expectedRecordsAffected = notMatchedBehavior == BulkMergeNotMatchedBehavior.DoNothing ? numUpdate + numAdd : numUpdate + numNotMatched + numAdd; using (var db = new postgresContext()) { var numRowsAffected = await db.Efcoretest .Where(e => e.Name.Contains(nameof(BulkMergeTests))) .BulkMergeAsync( toMerge, notMatchedBehavior: notMatchedBehavior, whenNotMatched: () => new Efcoretest { Name = $"{nameof(BulkMergeTests)}__Archived__" }); Assert.Equal(expectedRecordsAffected, numRowsAffected); var dbCount = await db.Efcoretest .Where(e => e.Name.Contains(nameof(BulkMergeTests))) .CountAsync(); if (notMatchedBehavior == BulkMergeNotMatchedBehavior.Delete) { Assert.Equal(toMerge.Count, dbCount); } else { Assert.NotEqual(toMerge.Count, dbCount); } foreach (var m in toMerge) { var dbEntity = await db.Efcoretest.FindAsync(m.Id); Assert.NotNull(dbEntity); Assert.Equal(m.Name, dbEntity.Name); Assert.Equal(m.Modifieddate.ToString("d"), dbEntity.Modifieddate.ToString("d")); Assert.Equal(m.Createddate.ToString("d"), dbEntity.Createddate.ToString("d")); } var dontDeleteEntity = await db.Efcoretest.FindAsync(dontDelete.Id); Assert.NotNull(dontDeleteEntity); } }