Beispiel #1
0
        public void TestMultiDbBulkINserter()
        {
            System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseAlways <InsertContext>());
            AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory);
            var connectString =
                @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\TestMultiDbBulkInserter_{0}.mdf;Initial Catalog=multidbinserter_{1};Integrated Security=True;Connect Timeout=30";
            //init db
            int profileIdCount = 3;

            for (int i = 1; i <= profileIdCount; ++i)
            {
                var init    = new Init2();
                var context = new InsertContext(string.Format(connectString, i, i));
                context.Inits.Add(init);
                context.SaveChanges();
            }

            var profileDispatch  = new Func <Entity2, int>(e => e.ProfileId);
            var connectionGetter = new Func <int, string>(id => string.Format(connectString, id, id));

            var multiDbBulkInserter = new MultiDbBulkInserter <Entity2>(BlockContainerOptions.Default, profileDispatch,
                                                                        connectionGetter, "dbo.Seods", GSProduct.SEOD);

            var entities = new[]
            {
                new Entity2(1, 1f, "a"),
                new Entity2(2, 2f, "b"),
                new Entity2(2, 2f, "c"),
                new Entity2(3, 3f, "d"),
                new Entity2(3, 3f, "e"),
                new Entity2(3, 3f, "f")
            };

            foreach (var x in entities)
            {
                multiDbBulkInserter.InputBlock.Post(x);
            }
            multiDbBulkInserter.InputBlock.Complete();
            multiDbBulkInserter.CompletionTask.Wait();

            //assert result
            for (int i = 1; i <= profileIdCount; ++i)
            {
                var connString = connectionGetter(i);
                var curContext = new InsertContext(connString);
                Assert.AreEqual(i, curContext.Seods.Count());
                Assert.AreEqual(i, curContext.Seods.FirstOrDefault().Price);
            }
        }
Beispiel #2
0
        public void TestMultiDbBulkInserter()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways <InsertContext>());
            var connectString = TestUtils.GetLocalDBConnectionString("TestMultiDbBulkInserter_{0}");
            //init db
            int profileIdCount = 3;

            for (int i = 1; i <= profileIdCount; ++i)
            {
                var init    = new Init2();
                var context = new InsertContext(string.Format(connectString, i, i));
                context.Inits.Add(init);
                context.SaveChanges();
            }

            var profileDispatch  = new Func <Entity2, int>(e => e.ProfileId);
            var connectionGetter = new Func <int, string>(id => string.Format(connectString, id, id));

            var multiDbBulkInserter = new MultiDbBulkInserter <Entity2>(DataflowOptions.Default, profileDispatch,
                                                                        connectionGetter, "dbo.Seods", GSProduct.SEOD);

            var entities = new[]
            {
                new Entity2(1, 1f, "a"),
                new Entity2(2, 2f, "b"),
                new Entity2(2, 2f, "c"),
                new Entity2(3, 3f, "d"),
                new Entity2(3, 3f, "e"),
                new Entity2(3, 3f, "f")
            };

            multiDbBulkInserter.ProcessAsync(entities).Wait();

            //assert result
            for (int i = 1; i <= profileIdCount; ++i)
            {
                var connString = connectionGetter(i);
                var curContext = new InsertContext(connString);
                Assert.AreEqual(i, curContext.Seods.Count());
                Assert.AreEqual(i, curContext.Seods.FirstOrDefault().Price);
                Assert.IsTrue(curContext.Seods.FirstOrDefault().Name.Length == 1);
                Assert.IsTrue(curContext.Seods.FirstOrDefault().Name2.Length == 1);
            }
        }
        public void TestMultiDbBulkInserter()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<InsertContext>());
            var connectString = TestUtils.GetLocalDBConnectionString("TestMultiDbBulkInserter_{0}");
            //init db
            int profileIdCount = 3;
            for (int i = 1; i <= profileIdCount; ++i)
            {
                var init = new Init2();
                var context = new InsertContext(string.Format(connectString, i, i));
                context.Inits.Add(init);
                context.SaveChanges();
            }

            var profileDispatch = new Func<Entity2, int>(e => e.ProfileId);
            var connectionGetter = new Func<int, string>(id => string.Format(connectString, id, id));

            var multiDbBulkInserter = new MultiDbBulkInserter<Entity2>(DataflowOptions.Default, profileDispatch,
                connectionGetter, "dbo.Seods", GSProduct.SEOD);

            var entities = new[]
            {
                new Entity2(1, 1f, "a"),
                new Entity2(2, 2f, "b"),
                new Entity2(2, 2f, "c"),
                new Entity2(3, 3f, "d"),
                new Entity2(3, 3f, "e"),
                new Entity2(3, 3f, "f")
            };

            multiDbBulkInserter.ProcessAsync(entities).Wait();

            //assert result
            for (int i = 1; i <= profileIdCount; ++i)
            {
                var connString = connectionGetter(i);
                var curContext = new InsertContext(connString);
                Assert.AreEqual(i, curContext.Seods.Count());
                Assert.AreEqual(i, curContext.Seods.FirstOrDefault().Price);
                Assert.IsTrue(curContext.Seods.FirstOrDefault().Name.Length == 1);
                Assert.IsTrue(curContext.Seods.FirstOrDefault().Name2.Length == 1);
            }
        }