Beispiel #1
0
 public void Setup()
 {
     _db         = new TestContext();
     _randomizer = new BookRandomizer();
     Database.SetInitializer(new DatabaseInitialiser());
     _db.Database.Initialize(true);
 }
        [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));
            }
        }