Beispiel #1
0
        public void TestReferenceLoopDepth()
        {
            AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory);
            var connectString =
                @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\loop.mdf;Initial Catalog=loop;Integrated Security=True;Connect Timeout=30";

            //init database
            System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseAlways <LoopContext>());
            var context = new LoopContext(connectString);

            context.LoopInits.Add(new LoopInit());
            context.SaveChanges();

            var loopA = new LoopA
            {
                Name  = "loopA",
                LoopB = new LoopB
                {
                    Name  = "loopB",
                    LoopC = new LoopC
                    {
                        LoopA = new LoopA
                        {
                            Name = "loopA2"
                        }
                    }
                }
            };

            using (var connection = new SqlConnection(connectString))
            {
                connection.Open();

                var tableName = "dbo.LoopEntities";
                var accessor  = TypeAccessorManager <LoopA> .GetAccessorByDestLabel(GSProduct.AD, connectString, tableName);

                var reader = new BulkDataReader <LoopA>(accessor, new[] { loopA });
                using (var bulkCopy = new SqlBulkCopy(connection))
                {
                    foreach (SqlBulkCopyColumnMapping map in reader.ColumnMappings)
                    {
                        bulkCopy.ColumnMappings.Add(map);
                    }
                    bulkCopy.DestinationTableName = tableName;

                    bulkCopy.WriteToServer(reader);
                }
            }

            Assert.AreEqual("loopA", context.LoopEntities.FirstOrDefault().Name);
        }
        public void TestReferenceLoopDepth()
        {
            var connectString = TestUtils.GetLocalDBConnectionString();

            //init database
            Database.SetInitializer(new DropCreateDatabaseAlways <LoopContext>());
            var context = new LoopContext(connectString);

            context.LoopInits.Add(new LoopInit());
            context.SaveChanges();

            var loopA = new LoopA
            {
                Name  = "loopA",
                LoopB = new LoopB
                {
                    Name  = "loopB",
                    LoopC = new LoopC
                    {
                        LoopA = new LoopA
                        {
                            Name = "loopA2"
                        }
                    }
                }
            };

            using (var connection = new SqlConnection(connectString))
            {
                connection.Open();

                var tableName = "dbo.LoopEntities";
                var accessor  = TypeAccessorManager <LoopA> .GetAccessorForTable(new TargetTable(GSProduct.AD, connectString, tableName));

                var reader = new BulkDataReader <LoopA>(accessor, new[] { loopA });
                using (var bulkCopy = new SqlBulkCopy(connection))
                {
                    foreach (SqlBulkCopyColumnMapping map in reader.ColumnMappings)
                    {
                        bulkCopy.ColumnMappings.Add(map);
                    }
                    bulkCopy.DestinationTableName = tableName;

                    bulkCopy.WriteToServer(reader);
                }
            }

            Assert.AreEqual("loopA", context.LoopEntities.FirstOrDefault().Name);
        }
        public void TestReferenceLoopDepth()
        {
            var connectString = TestUtils.GetLocalDBConnectionString();
            //init database
            Database.SetInitializer(new DropCreateDatabaseAlways<LoopContext>());
            var context = new LoopContext(connectString);
            context.LoopInits.Add(new LoopInit());
            context.SaveChanges();

            var loopA = new LoopA
            {
                Name = "loopA",
                LoopB = new LoopB
                {
                    Name = "loopB",
                    LoopC = new LoopC
                    {
                        LoopA = new LoopA
                        {
                            Name = "loopA2"
                        }
                    }
                }
            };

            using (var connection = new SqlConnection(connectString))
            {
                connection.Open();

                var tableName = "dbo.LoopEntities";
                var accessor = TypeAccessorManager<LoopA>.GetAccessorForTable(new TargetTable(GSProduct.AD, connectString, tableName));
                var reader = new BulkDataReader<LoopA>(accessor, new[] { loopA });
                using (var bulkCopy = new SqlBulkCopy(connection))
                {
                    foreach (SqlBulkCopyColumnMapping map in reader.ColumnMappings)
                    {
                        bulkCopy.ColumnMappings.Add(map);
                    }
                    bulkCopy.DestinationTableName = tableName;

                    bulkCopy.WriteToServer(reader);
                }
            }

            Assert.AreEqual("loopA", context.LoopEntities.FirstOrDefault().Name);

        }