public void SqlBulkTools_BulkInsertWithColumnMappings_CorrectlyMapsColumns() { BulkOperations bulk = new BulkOperations(); List <CustomColumnMappingTest> col = new List <CustomColumnMappingTest>(); for (int i = 0; i < 30; i++) { col.Add(new CustomColumnMappingTest() { NaturalId = i, ColumnXIsDifferent = "ColumnX " + i, ColumnYIsDifferentInDatabase = i }); } _db.CustomColumnMappingTest.RemoveRange(_db.CustomColumnMappingTest.ToList()); _db.SaveChanges(); bulk.Setup <CustomColumnMappingTest>() .ForCollection(col) .WithTable("CustomColumnMappingTests") .AddAllColumns() .CustomColumnMapping(x => x.ColumnXIsDifferent, "ColumnX") .CustomColumnMapping(x => x.ColumnYIsDifferentInDatabase, "ColumnY") .BulkInsert(); bulk.CommitTransaction("SqlBulkToolsTest"); // Assert Assert.IsTrue(_db.CustomColumnMappingTest.Any()); }
public void SqlBulkTools_UpdateQuery_SetPriceOnSingleEntity() { _db.Books.RemoveRange(_db.Books.ToList()); _db.SaveChanges(); BulkOperations bulk = new BulkOperations(); List <Book> books = _randomizer.GetRandomCollection(30); var bookToTest = books[5]; bookToTest.Price = 50; var isbn = bookToTest.ISBN; bulk.Setup <Book>() .ForCollection(books) .WithTable("Books") .AddAllColumns() .BulkInsert(); bulk.CommitTransaction("SqlBulkToolsTest"); // Update price to 100 bulk.Setup <Book>() .ForSimpleUpdateQuery(new Book() { Price = 100 }) .WithTable("Books") .AddColumn(x => x.Price) .Update() .Where(x => x.ISBN == isbn); int updatedRecords = bulk.CommitTransaction("SqlBulkToolsTest"); Assert.IsTrue(updatedRecords == 1); Assert.AreEqual(100, _db.Books.Single(x => x.ISBN == isbn).Price); }
public async Task SqlBulkTools_UpdateQuery_SetPriceOnSingleEntity() { _db.Books.RemoveRange(_db.Books.ToList()); _db.SaveChanges(); BulkOperations bulk = new BulkOperations(); List <Book> books = _randomizer.GetRandomCollection(30); var bookToTest = books[5]; bookToTest.Price = 50; var isbn = bookToTest.ISBN; int updatedRecords = 0; using (TransactionScope trans = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { using (SqlConnection conn = new SqlConnection(ConfigurationManager .ConnectionStrings["SqlBulkToolsTest"].ConnectionString)) { await bulk.Setup <Book>() .ForCollection(books) .WithTable("Books") .AddAllColumns() .BulkInsert() .CommitAsync(conn); // Update price to 100 updatedRecords = await bulk.Setup <Book>() .ForObject(new Book() { Price = 100 }) .WithTable("Books") .AddColumn(x => x.Price) .Update() .Where(x => x.ISBN == isbn) .CommitAsync(conn); } trans.Complete(); } Assert.IsTrue(updatedRecords == 1); Assert.AreEqual(100, _db.Books.Single(x => x.ISBN == isbn).Price); }
[Test] // For proof of concept public void EntityFrameworkAndSqlBulkToolsTogether_RollsbackGracefully() { BulkOperations bulk = new BulkOperations(); _db.Books.RemoveRange(_db.Books.ToList()); _db.SaveChanges(); string testIsbn = "1234567"; try { using (TransactionScope tx = new TransactionScope()) { using (SqlConnection conn = new SqlConnection(ConfigurationManager .ConnectionStrings["SqlBulkToolsTest"].ConnectionString)) { // Use the same connection for EntityFramework using (TestContext testContext = new TestContext(conn, contextOwnsConnection: false)) { testContext.Books.Add(new Book() { BestSeller = true, Description = "I am a valid insert", Title = "Hello World", ISBN = testIsbn, Price = 23.99M }); /* This is a valid insert. Nothing wrong with it but if any transaction within TransactionScope fails, * we want to undo this transaction. */ testContext.SaveChanges(); } bulk.Setup <Book>() .ForObject(new Book() { BestSeller = true, Description = "I'm not a valid insert, therefore everything should rollback", Title = "Hello World", ISBN = testIsbn, Price = 23.99M }) .WithTable("Books") .AddAllColumns() .Upsert() .SetIdentityColumn(x => x.Id, ColumnDirectionType.InputOutput) //.MatchTargetOn(x => x.Id) This will throw an exception (intentionally). MatchTargetOn can't be null. .Commit(conn); } // We will never reach this statement. tx.Complete(); } } catch (NullReferenceException e) { Assert.AreEqual("MatchTargetOn is a mandatory for upsert operation", e.Message); Assert.IsNull(_db.Books.SingleOrDefault(x => x.ISBN == testIsbn)); } }