public void TestNestClass()
        {
            var connectString = TestUtils.GetLocalDBConnectionString();
            Database.SetInitializer(new DropCreateDatabaseAlways<NestContext>());
            var context = new NestContext(connectString);
            context.NestInits.Add(new NestInit());
            context.SaveChanges();

            var a1 = new ABulk {};
            var a2 = new ABulk {B = new B { } };
            var a3 = new ABulk {B = new B { Value2 = "a" } };
            var a4 = new ABulk {Value1 = "a", B = new B { Value2 = "a" } };
            
            using (var connection = new SqlConnection(connectString))
            {
                connection.Open();
                var aTypeAccessor = TypeAccessorManager<ABulk>.GetAccessorForTable(new TargetTable("NestClassTest", connectString, "dbo.AEFs"));
                var aReader = new BulkDataReader<ABulk>(aTypeAccessor, new[] { a1, a2, a3, a4 });
                using (var aCopy = new SqlBulkCopy(connection))
                {
                    foreach (var map in aReader.ColumnMappings)
                    {
                        aCopy.ColumnMappings.Add(map);
                    }
                    aCopy.DestinationTableName = "dbo.AEFs";
                    aCopy.WriteToServer(aReader);
                }
            }

            Assert.AreEqual(1, context.AAs.FirstOrDefault().Id);
            Assert.AreEqual(1, context.AAs.Count(a => a.Value1 == "a"));
            Assert.AreEqual(3, context.AAs.Count(a => a.Value1 == "-"));
            Assert.AreEqual(2, context.AAs.Count(a => a.Value2 == "a"));
            Assert.AreEqual(2, context.AAs.Count(a => a.Value2 == "--"));
        }
示例#2
0
        public void TestNestClass()
        {
            var connectString = TestUtils.GetLocalDBConnectionString();

            Database.SetInitializer(new DropCreateDatabaseAlways <NestContext>());
            var context = new NestContext(connectString);

            context.NestInits.Add(new NestInit());
            context.SaveChanges();

            var a1 = new ABulk {
            };
            var a2 = new ABulk {
                B = new B {
                }
            };
            var a3 = new ABulk {
                B = new B {
                    Value2 = "a"
                }
            };
            var a4 = new ABulk {
                Value1 = "a", B = new B {
                    Value2 = "a"
                }
            };

            using (var connection = new SqlConnection(connectString))
            {
                connection.Open();
                var aTypeAccessor = TypeAccessorManager <ABulk> .GetAccessorForTable(new TargetTable("NestClassTest", connectString, "dbo.AEFs"));

                var aReader = new BulkDataReader <ABulk>(aTypeAccessor, new[] { a1, a2, a3, a4 });
                using (var aCopy = new SqlBulkCopy(connection))
                {
                    foreach (var map in aReader.ColumnMappings)
                    {
                        aCopy.ColumnMappings.Add(map);
                    }
                    aCopy.DestinationTableName = "dbo.AEFs";
                    aCopy.WriteToServer(aReader);
                }
            }

            Assert.AreEqual(1, context.AAs.FirstOrDefault().Id);
            Assert.AreEqual(1, context.AAs.Count(a => a.Value1 == "a"));
            Assert.AreEqual(3, context.AAs.Count(a => a.Value1 == "-"));
            Assert.AreEqual(2, context.AAs.Count(a => a.Value2 == "a"));
            Assert.AreEqual(2, context.AAs.Count(a => a.Value2 == "--"));
        }
示例#3
0
        public void TestNestClass()
        {
            AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory);
            var connectString =
                @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\nestClassTest.mdf;Initial Catalog=nestClassTest;Integrated Security=True;Connect Timeout=30";

            System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseAlways <NestContext>());
            var context = new NestContext(connectString);

            context.NestInits.Add(new NestInit());
            context.SaveChanges();

            var a = new A
            {
                AId = 1,
                B   = new B
                {
                    BId = 2,
                    A   = new A {
                        AId = 3
                    }
                }
            };

            var b = new B
            {
                BId = 1,
                A   = new A
                {
                    AId = 2,
                    B   = new B {
                        BId = 3
                    }
                }
            };

            using (var connection = new SqlConnection(connectString))
            {
                connection.Open();
                var aTypeAccessor = TypeAccessorManager <A> .GetAccessorByDestLabel("test1", connectString, "dbo.AAs");

                var aReader = new BulkDataReader <A>(aTypeAccessor, new[] { a });
                using (var aCopy = new SqlBulkCopy(connection))
                {
                    foreach (var map in aReader.ColumnMappings)
                    {
                        aCopy.ColumnMappings.Add(map);
                    }
                    aCopy.DestinationTableName = "dbo.AAs";
                    aCopy.WriteToServer(aReader);
                }

                var bTypeAccessor = TypeAccessorManager <B> .GetAccessorByDestLabel("test2", connectString, "dbo.BBs");

                var bReader = new BulkDataReader <B>(bTypeAccessor, new[] { b });
                using (var bCopy = new SqlBulkCopy(connection))
                {
                    foreach (var map in bReader.ColumnMappings)
                    {
                        bCopy.ColumnMappings.Add(map);
                    }
                    bCopy.DestinationTableName = "dbo.BBs";
                    bCopy.WriteToServer(aReader);
                }
            }

            Assert.AreEqual(1, context.AAs.FirstOrDefault().AId);
            Assert.AreEqual(1, context.BBs.FirstOrDefault().BId);
        }