public void BulkAdd(IEnumerable <T> entities) { using (var db = new SqlDbContext(_options)) { db.AddRange(entities); db.SaveChanges(); } }
//------------------------------------------------------------------ //private methods private void CreateContextAndWriteBatch(DbContextOptions <SqlDbContext> options, List <Book> batch) { //if (_updater == null) // throw new InvalidOperationException("The NoSql updater is null. This can be caused by the NoSql connection string being null or empty."); using (var context = new SqlDbContext(options)) { //need to set the key of the authors entities. They aren't tarcked but the add will sort out whether to add/Unchanged based on primary key foreach (var dbAuthor in context.Authors.ToList()) { if (_authorDict.ContainsKey(dbAuthor.Name)) { _authorDict[dbAuthor.Name].AuthorId = dbAuthor.AuthorId; } } context.AddRange(batch); context.SaveChanges(); //Now we update the NoSql database //SendBatchToNoSql(batch); } }
public async Task WriteBooksAsync(string filePath, int totalBooksNeeded, bool makeBookTitlesDistinct, CancellationToken cancellationToken) { _loadedBookData = JsonConvert.DeserializeObject <List <BookData> >(File.ReadAllText(filePath)) .ToImmutableList(); //Find out how many in db so we can pick up where we left off int numBooksInDb; using (var context = new SqlDbContext(_sqlOptions, null)) { numBooksInDb = await context.Books.IgnoreQueryFilters().CountAsync(); } var numWritten = 0; var numToWrite = totalBooksNeeded - numBooksInDb; while (numWritten < numToWrite) { if (cancellationToken.IsCancellationRequested) { return; } //noSql can be null. If so then it doesn't write to CosmosDb var noSqlBookUpdater = _noSqlOptions != null ? new NoSqlBookUpdater(new NoSqlDbContext(_noSqlOptions)) : null; using (var sqlDbContext = new SqlDbContext(_sqlOptions, noSqlBookUpdater)) { var authorsFinder = new AuthorFinder(sqlDbContext); var batchToAdd = Math.Min(_loadedBookData.Count, numToWrite - numWritten); var batch = GenerateBooks(batchToAdd, numBooksInDb, makeBookTitlesDistinct, authorsFinder).ToList(); sqlDbContext.AddRange(batch); await sqlDbContext.SaveChangesAsync(); numWritten += batch.Count; numBooksInDb += batch.Count; } } }