Beispiel #1
0
        public void test_transaction_subscription_rollback()
        {
            using (var session = NHConfig.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var beforeCommitExecuted = false;
                    var afterCommitExecuted  = false;

                    // BeforeCommit wont be executed on rollback
                    session.Subscribe(o => o.Transaction
                                      .BeforeCommit(s =>
                    {
                        Assert.AreEqual(session, s);
                        Assert.IsTrue(s.Transaction.IsActive);
                        beforeCommitExecuted = true;
                    })
                                      .AfterCommit((s, success) =>
                    {
                        Assert.IsFalse(success);
                        Assert.AreEqual(session, s);
                        Assert.IsFalse(s.Transaction.IsActive);
                        afterCommitExecuted = true;
                    }));

                    Assert.IsFalse(beforeCommitExecuted);
                    Assert.IsFalse(afterCommitExecuted);
                    transaction.Rollback();
                    Assert.IsFalse(beforeCommitExecuted);
                    Assert.IsTrue(afterCommitExecuted);
                }
        }
        public void batch_performance()
        {
            var keys       = Enumerable.Range(1, 5000).ToList();
            var batchSizes = new [] { 250, 1, 10, 50, 100, 250, 500, 1000 };
            var coldStart  = true; // skip the first time as the time is always higher

            foreach (var batchSize in batchSizes)
            {
                using (var session = NHConfig.OpenSession())
                {
                    var stats      = session.SessionFactory.Statistics;
                    var queryCount = stats.PrepareStatementCount;
                    var stopwatch  = new Stopwatch();
                    stopwatch.Start();
                    var models = session.BatchFetch <BatchModel, int>(keys, o => o.Id, batchSize);
                    stopwatch.Stop();
                    if (coldStart)
                    {
                        coldStart = false;
                    }
                    else
                    {
                        Console.WriteLine($"Elapsed time for batch size {batchSize}: {stopwatch.ElapsedMilliseconds}ms");
                    }
                    var expectedQueryCount = (int)Math.Ceiling(keys.Count / (decimal)batchSize);
                    Assert.AreEqual(5000, models.Count);
                    Assert.AreEqual(expectedQueryCount, stats.PrepareStatementCount - queryCount);

                    foreach (var model in models)
                    {
                        Assert.IsTrue(keys.Contains(model.Id));
                    }
                }
            }
        }
        public void deep_clone_filter()
        {
            EQBPerson clone;
            EQBPerson petra;

            using (var session = NHConfig.OpenSession())
            {
                petra = session.Query <EQBPerson>()
                        .Include(o => o.PreviouslyOwnedVehicles)
                        .First(o => o.Name == "Petra");

                clone = session.DeepClone(petra, o => o
                                          .ForType <EQBPerson>(t => t
                                                               .ForMember(m => m.Name, m => m.Filter(n => n + "2"))
                                                               .ForMember(m => m.PreviouslyOwnedVehicles, m => m
                                                                          .Filter(col => new HashSet <EQBVehicle>(col.Take(1)))
                                                                          )
                                                               ));
            }

            Assert.AreEqual("Petra2", clone.Name);
            Assert.IsNull(clone.BestFriend);
            Assert.IsNull(clone.IdentityCard);
            Assert.IsNull(clone.MarriedWith);
            Assert.AreEqual(0, clone.CurrentOwnedVehicles.Count);
            Assert.AreEqual(1, clone.PreviouslyOwnedVehicles.Count);
        }
        public void batch_test_with_int_property()
        {
            var keys = new HashSet <int>();
            var r    = new Random();

            for (var i = 0; i < 600; i++)
            {
                keys.Add(r.Next(1, 1200));
            }

            using (var session = NHConfig.OpenSession())
            {
                var stats      = session.SessionFactory.Statistics;
                var queryCount = stats.PrepareStatementCount;
                var models     = session.BatchFetch <BatchModel, int>(keys.ToList(), o => o.Id, 50);

                var expectedQueryCount = (int)Math.Ceiling(keys.Count / 50m);
                Assert.AreEqual(keys.Count, models.Count);
                Assert.AreEqual(expectedQueryCount, stats.PrepareStatementCount - queryCount);

                foreach (var model in models)
                {
                    Assert.IsTrue(keys.Contains(model.Id));
                }
            }
        }
        public void deep_clone_references()
        {
            EQBPerson clone;
            EQBPerson petra;

            using (var session = NHConfig.OpenSession())
            {
                petra = session.Query <EQBPerson>()
                        .Include(o => o.BestFriend.IdentityCard)
                        .Include(o => o.BestFriend.BestFriend)
                        .First(o => o.Name == "Petra");

                clone = session.DeepClone(petra);
            }

            Assert.AreEqual(petra.Id, clone.Id);
            Assert.IsNotNull(clone.BestFriend);
            Assert.IsNotNull(clone.BestFriend.IdentityCard);
            Assert.AreEqual(clone.BestFriend, clone.BestFriend.IdentityCard.Owner);
            Assert.IsNotNull(clone.BestFriend.BestFriend);

            Assert.IsNull(clone.BestFriend.BestFriend.BestFriend);
            Assert.IsNull(clone.IdentityCard);
            Assert.AreEqual(0, clone.OwnedHouses.Count);
            Assert.AreEqual(0, clone.CurrentOwnedVehicles.Count);
        }
        public void deep_clone_without_identifier()
        {
            EQBPerson clone;

            using (var session = NHConfig.OpenSession())
            {
                var petra = session.Query <EQBPerson>()
                            .Include(o => o.IdentityCard)
                            .Include(o => o.BestFriend.IdentityCard)
                            .Include(o => o.CurrentOwnedVehicles.First().Wheels)
                            .Include(o => o.CurrentOwnedVehicles.First().RoadworthyTests)
                            .Include(o => o.CurrentOwnedVehicles.First().MileageHistory)
                            .First(o => o.Name == "Petra");

                clone = session.DeepClone(petra, o => o
                                          .CloneIdentifier(false));
            }

            Assert.AreEqual(0, clone.Id);
            Assert.AreEqual(0, clone.IdentityCard.Id);
            Assert.AreEqual(0, clone.IdentityCard.Owner.Id);
            Assert.AreEqual(0, clone.BestFriend.Id);
            Assert.AreEqual(0, clone.BestFriend.IdentityCard.Id);
            Assert.AreEqual(1, clone.CurrentOwnedVehicles.Count);
            Assert.AreEqual(0, clone.CurrentOwnedVehicles.First().Id);
            Assert.AreEqual(0, clone.CurrentOwnedVehicles.First().CurrentOwner.Id);
            Assert.AreEqual(4, clone.CurrentOwnedVehicles.First().Wheels.Count);
            Assert.AreEqual(0, clone.CurrentOwnedVehicles.First().Wheels.First().Id);
            Assert.AreEqual(0, clone.CurrentOwnedVehicles.First().Wheels.First().Vehicle.Id);
            Assert.AreEqual(2, clone.CurrentOwnedVehicles.First().RoadworthyTests.Count);
            Assert.AreEqual(0, clone.CurrentOwnedVehicles.First().RoadworthyTests.First().Value.Id);
            Assert.AreEqual(0, clone.CurrentOwnedVehicles.First().RoadworthyTests.First().Value.Vehicle.Id);
        }
        public void using_then_include()
        {
            EQBPerson petra;

            using (var session = NHConfig.OpenSession())
            {
                var stats = session.SessionFactory.Statistics;
                stats.Clear();
                var future = session.Query <EQBPerson>()
                             .Include(o => o.BestFriend.IdentityCard)
                             .Include(o => o.BestFriend.BestFriend.BestFriend.BestFriend)
                             .Include(o => o.CurrentOwnedVehicles).ThenInclude(o => o.Wheels)
                             .Include(o => o.CurrentOwnedVehicles).ThenInclude(o => o.RoadworthyTests)
                             .Include(o => o.CurrentOwnedVehicles).ThenInclude(o => o.MileageHistory)
                             .Include(o => o.DrivingLicence)
                             .Include(o => o.CreatedBy)
                             .Include(o => o.IdentityCard)
                             .Include(o => o.MarriedWith)
                             .Include(o => o.OwnedHouses)
                             .Include(o => o.PreviouslyOwnedVehicles)
                             .Where(o => o.Name == "Petra")
                             .ToFutureValue();
                petra = future.Value;
                CheckStatistics(stats, 5);
            }
            ValidateGetEntityResult(petra);
        }
        public void deep_clone_with_skip_entity_types()
        {
            EQBPerson clone;
            EQBPerson petra;

            using (var session = NHConfig.OpenSession())
            {
                petra = session.Query <EQBPerson>()
                        .Include(o => o.IdentityCard)
                        .Include(o => o.BestFriend.IdentityCard)
                        .Include(o => o.CurrentOwnedVehicles.First().Wheels)
                        .Include(o => o.CurrentOwnedVehicles.First().RoadworthyTests)
                        .Include(o => o.CurrentOwnedVehicles.First().MileageHistory)
                        .First(o => o.Name == "Petra");

                clone = session.DeepClone(petra, o => o
                                          .SkipEntityTypes());
            }

            Assert.IsNull(clone.IdentityCard);
            Assert.IsNull(clone.BestFriend);
            Assert.AreEqual(1, clone.CurrentOwnedVehicles.Count);
            Assert.IsNull(clone.CurrentOwnedVehicles.First().CurrentOwner);
            Assert.AreEqual(4, clone.CurrentOwnedVehicles.First().Wheels.Count);
            Assert.IsNull(clone.CurrentOwnedVehicles.First().Wheels.First().Vehicle);
            Assert.AreEqual(2, clone.CurrentOwnedVehicles.First().RoadworthyTests.Count);
            Assert.IsNull(clone.CurrentOwnedVehicles.First().RoadworthyTests.First().Value.Vehicle);
            Assert.AreEqual(2, clone.CurrentOwnedVehicles.First().MileageHistory.Count);
            Assert.IsNotNull(clone.CurrentOwnedVehicles.First().MileageHistory[new DateTime(2010, 1, 1)]);
        }
        public void deep_clone_collections()
        {
            EQBPerson clone;
            EQBPerson petra;

            using (var session = NHConfig.OpenSession())
            {
                petra = session.Query <EQBPerson>()
                        .Include(o => o.CurrentOwnedVehicles.First().Wheels)
                        .Include(o => o.CurrentOwnedVehicles.First().RoadworthyTests)
                        .Include(o => o.CurrentOwnedVehicles.First().MileageHistory)
                        .Include(o => o.PreviouslyOwnedVehicles)
                        .First(o => o.Name == "Petra");

                clone = session.DeepClone(petra);
            }

            Assert.IsNull(clone.BestFriend);
            Assert.IsNull(clone.IdentityCard);
            Assert.IsNull(clone.MarriedWith);
            Assert.AreEqual(1, clone.CurrentOwnedVehicles.Count);
            Assert.AreEqual(clone, clone.CurrentOwnedVehicles.First().CurrentOwner);
            Assert.AreEqual(4, clone.CurrentOwnedVehicles.First().Wheels.Count);
            Assert.AreEqual(clone.CurrentOwnedVehicles.First(), clone.CurrentOwnedVehicles.First().Wheels.First().Vehicle);
            Assert.AreEqual(2, clone.CurrentOwnedVehicles.First().RoadworthyTests.Count);
            Assert.AreEqual(clone.CurrentOwnedVehicles.First(), clone.CurrentOwnedVehicles.First().RoadworthyTests[new DateTime(2009, 2, 1)].Vehicle);
            Assert.AreEqual(2, clone.CurrentOwnedVehicles.First().MileageHistory.Count);
            Assert.AreEqual(5000, clone.CurrentOwnedVehicles.First().MileageHistory[new DateTime(2010, 1, 1)]);

            Assert.AreEqual(2, clone.PreviouslyOwnedVehicles.Count);
            Assert.AreEqual(clone, clone.CurrentOwnedVehicles.First().CurrentOwner);
        }
        public async Task get_single_result_without_skip_or_take_with_first_async()
        {
            EQBPerson petra;

            using (var session = NHConfig.OpenSession())
            {
                var stats = session.SessionFactory.Statistics;
                stats.Clear();
                petra = await session.Query <EQBPerson>()
                        .Include(o => o.BestFriend.IdentityCard)
                        .Include(o => o.BestFriend.BestFriend.BestFriend.BestFriend)
                        .Include(o => o.CurrentOwnedVehicles.First().Wheels)
                        .Include(o => o.CurrentOwnedVehicles.First().RoadworthyTests)
                        .Include(o => o.CurrentOwnedVehicles.First().MileageHistory)
                        .Include(o => o.DrivingLicence)
                        .Include(o => o.CreatedBy)
                        .Include(o => o.IdentityCard)
                        .Include(o => o.MarriedWith)
                        .Include(o => o.OwnedHouses)
                        .Include(o => o.PreviouslyOwnedVehicles)
                        .Where(o => o.Name == "Petra")
                        .FirstAsync();

                CheckStatistics(stats, 5);
            }
            ValidateGetEntityResult(petra);
        }
        public void deep_clone_simple_properties()
        {
            EQBPerson clone;
            EQBPerson petra;

            using (var session = NHConfig.OpenSession())
            {
                petra = session.Query <EQBPerson>()
                        .First(o => o.Name == "Petra");
                clone = session.DeepClone(petra, o => o
                                          .ForType <EQBPerson>(t =>
                                                               t.ForMember(m => m.Name, opts => opts.Ignore())
                                                               ));
                // Lazy load some relations after cloning
                var friend = petra.BestFriend;
                var card   = petra.IdentityCard;
            }
            Assert.AreEqual(petra.Id, clone.Id);
            Assert.AreEqual(null, clone.Name);
            Assert.AreEqual(petra.LastName, clone.LastName);
            Assert.IsNotNull(petra.BestFriend);
            Assert.IsNotNull(petra.IdentityCard);
            Assert.IsNull(clone.MarriedWith);
            Assert.IsNull(clone.BestFriend);
            Assert.IsNull(clone.IdentityCard);
            Assert.AreEqual(0, clone.OwnedHouses.Count);
            Assert.AreEqual(0, clone.CurrentOwnedVehicles.Count);
        }
        public void using_skip_and_take()
        {
            /*NHibernate way*/
            using (var session = NHConfig.OpenSession())
            {
                var subQuery = session.Query <EQBPerson>()
                               .Skip(0)
                               .Take(10);
                var query = session.Query <EQBPerson>()
                            .Fetch(o => o.BestFriend)
                            .ThenFetch(o => o.IdentityCard)
                            .Fetch(o => o.BestFriend)
                            .ThenFetch(o => o.BestFriend)
                            .ThenFetch(o => o.BestFriend)
                            .ThenFetch(o => o.BestFriend)
                            .FetchMany(o => o.CurrentOwnedVehicles)
                            .ThenFetchMany(o => o.Wheels)
                            .FetchMany(o => o.CurrentOwnedVehicles)
                            .ThenFetchMany(o => o.RoadworthyTests)
                            .FetchMany(o => o.CurrentOwnedVehicles)
                            .ThenFetchMany(o => o.MileageHistory)
                            .Fetch(o => o.DrivingLicence)
                            .Fetch(o => o.IdentityCard)
                            .Fetch(o => o.MarriedWith)
                            .Where(o => subQuery.Contains(o))
                            .ToFuture();
                session.Query <EQBPerson>()
                .FetchMany(o => o.OwnedHouses)
                .Where(o => subQuery.Contains(o))
                .ToFuture();
                session.Query <EQBPerson>()
                .FetchMany(o => o.PreviouslyOwnedVehicles)
                .Where(o => subQuery.Contains(o))
                .ToFuture();
                Assert.AreEqual(4, query.ToList().Count);
            }


            using (var session = NHConfig.OpenSession())
            {
                var test = session.Query <EQBPerson>()
                           .Skip(0)
                           .Take(10)
                           .Include(o => o.BestFriend.IdentityCard)
                           .Include(o => o.BestFriend.BestFriend.BestFriend.BestFriend)
                           .Include(o => o.CurrentOwnedVehicles)
                           .Include(o => o.CurrentOwnedVehicles).ThenInclude(o => o.Wheels)
                           .Include(o => o.CurrentOwnedVehicles).ThenInclude(o => o.RoadworthyTests)
                           .Include(o => o.CurrentOwnedVehicles).ThenInclude(o => o.MileageHistory)
                           .Include(o => o.DrivingLicence)
                           .Include(o => o.IdentityCard)
                           .Include(o => o.MarriedWith)
                           .Include(o => o.OwnedHouses)
                           .Include(o => o.PreviouslyOwnedVehicles)
                           .ToList();
                Assert.AreEqual(4, test.Count);
            }
        }
        public void test_include_with_collection()
        {
            EQBPerson petra;

            /*Without parameter*/
            using (var session = NHConfig.OpenSession())
            {
                var stats = session.SessionFactory.Statistics;
                stats.Clear();
                petra = session.Query <EQBPerson>()
                        .Include(o => o.PreviouslyOwnedVehicles)
                        .Single(o => o.Name == "Petra");
                CheckStatistics(stats, 1);
            }
        }
        public void test_include_with_interface()
        {
            EQBPerson petra;

            /*Without parameter*/
            using (var session = NHConfig.OpenSession())
            {
                var stats = session.SessionFactory.Statistics;
                stats.Clear();
                petra = session.Query <EQBPerson>()
                        .Include(o => o.CreatedBy)
                        .Single(o => o.Name == "Petra");
                CheckStatistics(stats, 1);
            }
            Assert.AreEqual("System", petra.CreatedBy.UserName);
        }
        public void immutable()
        {
            using (var session = NHConfig.OpenSession())
            {
                var query = session.Query <EQBPerson>().Include(o => o.BestFriend);

                Assert.AreEqual(typeof(IncludeQueryProvider), query.Provider.GetType());
                Assert.AreNotEqual(query.Provider, query.Include(o => o.DrivingLicence).Provider);
                Assert.AreNotEqual(query.Provider, query.Include(o => o.CurrentOwnedVehicles).Provider);
                Assert.AreNotEqual(query.Provider, query.Include("DrivingLicence").Provider);
                Assert.AreNotEqual(query.Provider, ((IQueryable)query).Include("DrivingLicence").Provider);

                var includeQuery = query.Include(o => o.CurrentOwnedVehicles);
                Assert.AreNotEqual(includeQuery.Provider, includeQuery.ThenInclude(o => o.CurrentOwner).Provider);
                Assert.AreNotEqual(includeQuery.Provider, includeQuery.ThenInclude(o => o.PreviousUsers).Provider);
            }
        }
        public void test_cast_to_base_type_relation()
        {
            IPerson petra;

            /*Without parameter*/
            using (var session = NHConfig.OpenSession())
            {
                var stats = session.SessionFactory.Statistics;
                stats.Clear();
                var query = session.Query <EQBPerson>()
                            .Where(o => o.Name == "Petra") as IQueryable;
                petra = query
                        .Include("CurrentOwnedVehicles")
                        .ToList <IPerson>().First();
                CheckStatistics(stats, 1);
            }
            Assert.AreEqual(petra.CurrentOwnedVehicles.Any(), true);
        }
        public void test_selectmany()
        {
            IPerson petra;

            /*Without parameter*/
            using (var session = NHConfig.OpenSession())
            {
                var stats = session.SessionFactory.Statistics;
                stats.Clear();
                var query = session.Query <EQBIdentityCard>()
                            .Where(o => o.Code == "SD")
                            .Fetch(o => o.Owner).ThenFetch(o => o.BestFriend).ThenFetch(o => o.MarriedWith)
                            //.Select(o => o.Owner)
                            .ToList();

                Assert.AreEqual(1, stats.PrepareStatementCount);
            }
            //Assert.AreEqual("System", petra.CreatedBy.UserName);
        }
        public async Task using_first_async_method_for_retriving_a_person_that_dont_exists()
        {
            EQBPerson test;

            using (var session = NHConfig.OpenSession())
            {
                test = await session.Query <EQBPerson>()
                       .Include(o => o.BestFriend.IdentityCard)
                       .Include(o => o.BestFriend.BestFriend.BestFriend.BestFriend)
                       .Include(o => o.CurrentOwnedVehicles.First().Wheels)
                       .Include(o => o.CurrentOwnedVehicles.First().RoadworthyTests)
                       .Include(o => o.DrivingLicence)
                       .Include(o => o.IdentityCard)
                       .Include(o => o.MarriedWith)
                       .Include(o => o.OwnedHouses)
                       .Include(o => o.PreviouslyOwnedVehicles)
                       .FirstAsync(o => o.Name == "Test");
            }
        }
        public void collections_with_same_prefix()
        {
            EQBPerson person;

            using (var session = NHConfig.OpenSession())
            {
                var stats = session.SessionFactory.Statistics;
                stats.Clear();

                person = session.Query <EQBPerson>()
                         .Include(o => o.CurrentOwnedVehicles)
                         .Include(o => o.CurrentOwnedVehiclesOld)
                         .FirstOrDefault(o => o.Name == "Petra");

                CheckStatistics(stats, 2);
            }
            Assert.IsNotNull(person);
            Assert.AreEqual(1, person.CurrentOwnedVehicles.Count);
        }
        public void batch_test_select_anonymous_type()
        {
            var keys = Enumerable.Range(1, 600).ToList();

            using (var session = NHConfig.OpenSession())
            {
                var stats      = session.SessionFactory.Statistics;
                var queryCount = stats.PrepareStatementCount;
                var models     = session.BatchFetch <BatchModel>(50)
                                 .SetKeys(keys, o => o.Id)
                                 .BeforeQueryExecution(q => q.Where(o => o.Id > 400))
                                 .Select(o => new { o.Name })
                                 .Execute();

                var expectedQueryCount = (int)Math.Ceiling(keys.Count / 50m);
                Assert.AreEqual(200, models.Count);
                Assert.AreEqual(expectedQueryCount, stats.PrepareStatementCount - queryCount);
            }
        }
        public void test_cast_to_base_type()
        {
            IPerson petra;

            /*Without parameter*/
            using (var session = NHConfig.OpenSession())
            {
                var stats = session.SessionFactory.Statistics;
                stats.Clear();
                var query = session.Query <EQBPerson>() as IQueryable <IPerson>;
                petra = query
                        .Include("CreatedBy")
                        .Where(o => o.Name == "Petra")
                        .First();
                CheckStatistics(stats, 1);
            }
            Assert.IsNotNull(petra);
            Assert.AreEqual("System", petra.CreatedBy.UserName);
        }
        public void batch_test_with_filter()
        {
            var keys = Enumerable.Range(1, 600).ToList();

            using (var session = NHConfig.OpenSession())
            {
                var stats      = session.SessionFactory.Statistics;
                var queryCount = stats.PrepareStatementCount;
                var models     = session.BatchFetch <BatchModel, int>(keys, o => o.Id, 50, q => q.Where(o => o.Id > 400));

                var expectedQueryCount = (int)Math.Ceiling(keys.Count / 50m);
                Assert.AreEqual(200, models.Count);
                Assert.AreEqual(expectedQueryCount, stats.PrepareStatementCount - queryCount);

                foreach (var model in models)
                {
                    Assert.IsTrue(keys.Contains(model.Id));
                }
            }
        }
        public void using_tofutorevalue_method_for_retriving_a_person_that_dont_exists()
        {
            EQBPerson test;

            using (var session = NHConfig.OpenSession())
            {
                test = session.Query <EQBPerson>()
                       .Include(o => o.BestFriend.IdentityCard)
                       .Include(o => o.BestFriend.BestFriend.BestFriend.BestFriend)
                       .Include(o => o.CurrentOwnedVehicles.First().Wheels)
                       .Include(o => o.CurrentOwnedVehicles.First().RoadworthyTests)
                       .Include(o => o.DrivingLicence)
                       .Include(o => o.IdentityCard)
                       .Include(o => o.MarriedWith)
                       .Include(o => o.OwnedHouses)
                       .Include(o => o.PreviouslyOwnedVehicles)
                       .Where(o => o.Name == "Test")
                       .ToFutureValue().Value;
            }
            Assert.IsNull(test);
        }
        public async Task using_skip_and_take_async()
        {
            using (var session = NHConfig.OpenSession())
            {
                var test = await session.Query <EQBPerson>()
                           .Skip(0)
                           .Take(10)
                           .Include(o => o.BestFriend.IdentityCard)
                           .Include(o => o.BestFriend.BestFriend.BestFriend.BestFriend)
                           .Include(o => o.CurrentOwnedVehicles.First().Wheels)
                           .Include(o => o.CurrentOwnedVehicles.First().RoadworthyTests)
                           .Include(o => o.DrivingLicence)
                           .Include(o => o.IdentityCard)
                           .Include(o => o.MarriedWith)
                           .Include(o => o.OwnedHouses)
                           .Include(o => o.PreviouslyOwnedVehicles)
                           .ToListAsync();

                Assert.AreEqual(4, test.Count);
            }
        }
        public void using_count_method()
        {
            using (var session = NHConfig.OpenSession())
            {
                var query = session.Query <EQBPerson>()
                            .Include(o => o.BestFriend.IdentityCard)
                            .Include(o => o.BestFriend.BestFriend.BestFriend.BestFriend)
                            .Include(o => o.CurrentOwnedVehicles.First().Wheels)
                            .Include(o => o.CurrentOwnedVehicles.First().RoadworthyTests)
                            .Include(o => o.DrivingLicence)
                            .Include(o => o.IdentityCard)
                            .Include(o => o.MarriedWith)
                            .Include(o => o.OwnedHouses)
                            .Include(o => o.PreviouslyOwnedVehicles);

                var total = query.Count();
                Assert.AreEqual(4, total);

                var people = query.ToList();
                Assert.AreEqual(4, people.Count);
            }
        }
        public async Task using_to_future_value_async_method_without_getting_value()
        {
            EQBPerson test;

            using (var session = NHConfig.OpenSession())
            {
                var test2 = session.Query <EQBPerson>()
                            .Include(o => o.BestFriend.IdentityCard)
                            .Include(o => o.BestFriend.BestFriend.BestFriend.BestFriend)
                            .Include(o => o.CurrentOwnedVehicles.First().Wheels)
                            .Include(o => o.CurrentOwnedVehicles.First().RoadworthyTests)
                            .Include(o => o.DrivingLicence)
                            .Include(o => o.IdentityCard)
                            .Include(o => o.MarriedWith)
                            .Include(o => o.OwnedHouses)
                            .Include(o => o.PreviouslyOwnedVehicles)
                            .Where(o => o.Name == "Test")
                            .ToFutureValue();
                test = await test2.GetValueAsync();
            }
            Assert.IsNull(test);
        }
        public void deep_clone_as_reference_and_ignore_properties_identifiers()
        {
            EQBPerson clone;
            EQBPerson petra;

            using (var session = NHConfig.OpenSession())
            {
                petra = session.Query <EQBPerson>()
                        .Include(o => o.IdentityCard)
                        .First(o => o.Name == "Petra");
                clone = session.DeepClone(petra, o => o
                                          .ForType <EQBPerson>(t => t
                                                               .ForMember(m => m.Name, opts => opts.Ignore())
                                                               .CloneIdentifier(false)
                                                               )
                                          .CloneIdentifier(true)
                                          .CanCloneAsReference(type => type == typeof(EQBIdentityCard))
                                          );
            }
            Assert.AreEqual(default(int), clone.Id);
            Assert.IsNull(clone.Name);
            Assert.AreEqual(petra.LastName, clone.LastName);
            Assert.AreEqual(petra.IdentityCard, clone.IdentityCard);
        }
        protected void FillData()
        {
            var system = new EQBUser {
                UserName = "******"
            };
            var ana = new EQBPerson {
                Age = 23, Name = "Ana", CreatedBy = system
            };
            var rok = new EQBPerson {
                Age = 24, Name = "Rok", CreatedBy = system
            };
            var simon = new EQBPerson {
                Age = 25, Name = "Simon", CreatedBy = system
            };
            var petra = new EQBPerson {
                Age = 22, Name = "Petra", CreatedBy = system
            };

            //Setting best friends
            petra.BestFriend = ana;
            ana.BestFriend   = simon;
            simon.BestFriend = rok;
            rok.BestFriend   = petra;

            //Setting Identity card
            ana.IdentityCard = new EQBIdentityCard {
                Code = "1", Owner = ana
            };
            ana.Identity = new EQBIdentity {
                Code = "1", Owner = ana
            };
            rok.IdentityCard = new EQBIdentityCard {
                Code = "2", Owner = rok
            };
            rok.Identity = new EQBIdentity {
                Code = "2", Owner = rok
            };
            simon.IdentityCard = new EQBIdentityCard {
                Code = "3", Owner = simon
            };
            simon.Identity = new EQBIdentity {
                Code = "3", Owner = simon
            };
            petra.IdentityCard = new EQBIdentityCard {
                Code = "4", Owner = petra
            };
            petra.Identity = new EQBIdentity {
                Code = "4", Owner = petra
            };

            //Setting Driving licence
            rok.DrivingLicence = new EQBDrivingLicence {
                Code = "1", Owner = rok
            };
            simon.DrivingLicence = new EQBDrivingLicence {
                Code = "2", Owner = simon
            };
            petra.DrivingLicence = new EQBDrivingLicence {
                Code = "3", Owner = petra
            };

            //Setting MerriedWith
            rok.MarriedWith = ana;
            ana.MarriedWith = rok;

            petra.MarriedWith = simon;
            simon.MarriedWith = petra;

            //Setting Vehicles
            var ferrari = new EQBVehicle {
                BuildYear = 2002, Model = "Ferrari"
            };

            ferrari.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 320, Vehicle = ferrari
            });
            ferrari.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 320, Vehicle = ferrari
            });
            ferrari.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 260, Vehicle = ferrari
            });
            ferrari.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 260, Vehicle = ferrari
            });
            ferrari.RoadworthyTests.Add(
                new DateTime(2002, 2, 1),
                new EQBRoadworthyTest
            {
                Vehicle  = ferrari,
                TestDate = new DateTime(2002, 2, 1),
                Passed   = true,
                Comments = "I like the shade of red."
            });
            ferrari.MileageHistory.Add(new DateTime(2002, 1, 1), 0);
            ferrari.MileageHistory.Add(new DateTime(2006, 1, 1), 60000);
            ferrari.MileageHistory.Add(new DateTime(2010, 1, 1), 100000);

            var audi = new EQBVehicle {
                BuildYear = 2009, Model = "Audi"
            };

            audi.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 235, Vehicle = audi
            });
            audi.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 235, Vehicle = audi
            });
            audi.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 235, Vehicle = audi
            });
            audi.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 235, Vehicle = audi
            });
            audi.RoadworthyTests.Add(
                new DateTime(2009, 2, 1),
                new EQBRoadworthyTest
            {
                Vehicle  = audi,
                TestDate = new DateTime(2009, 2, 1),
                Passed   = false,
                Comments = "Brakes failing."
            });
            audi.RoadworthyTests.Add(
                new DateTime(2009, 3, 1),
                new EQBRoadworthyTest
            {
                Vehicle  = audi,
                TestDate = new DateTime(2009, 3, 1),
                Passed   = true,
                Comments = "All good now."
            });
            audi.MileageHistory.Add(new DateTime(2009, 1, 1), 0);
            audi.MileageHistory.Add(new DateTime(2010, 1, 1), 5000);

            var bmw = new EQBVehicle {
                BuildYear = 1993, Model = "Bmw"
            };

            bmw.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 205, Vehicle = bmw
            });
            bmw.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 205, Vehicle = bmw
            });
            bmw.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 205, Vehicle = bmw
            });
            bmw.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 205, Vehicle = bmw
            });
            // Deliberately no roadworthy tests or mileage history

            var vw = new EQBVehicle {
                BuildYear = 2002, Model = "Vw"
            };

            vw.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 195, Vehicle = vw
            });
            vw.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 195, Vehicle = vw
            });
            vw.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 195, Vehicle = vw
            });
            vw.Wheels.Add(new TestEQBWheel {
                Diameter = 45, Width = 195, Vehicle = vw
            });
            vw.RoadworthyTests.Add(
                new DateTime(2002, 3, 1),
                new EQBRoadworthyTest
            {
                Vehicle  = vw,
                TestDate = new DateTime(2002, 3, 1),
                Passed   = true,
                Comments = "No problems."
            });
            vw.MileageHistory.Add(new DateTime(2002, 1, 1), 0);
            vw.MileageHistory.Add(new DateTime(2015, 1, 1), 150000);

            petra.PreviouslyOwnedVehicles.Add(vw);
            petra.PreviouslyOwnedVehicles.Add(bmw);
            petra.CurrentOwnedVehicles.Add(audi);
            audi.CurrentOwner = petra;

            simon.PreviouslyOwnedVehicles.Add(bmw);
            simon.PreviouslyOwnedVehicles.Add(audi);
            simon.CurrentOwnedVehicles.Add(ferrari);
            ferrari.CurrentOwner = simon;

            //Setting Houses
            var house1 = new EQBHouse {
                Address = "Address1"
            };
            var house2 = new EQBHouse {
                Address = "Address2"
            };

            house1.Owners.Add(ana);
            ana.OwnedHouses.Add(house1);
            house1.Owners.Add(rok);
            rok.OwnedHouses.Add(house1);

            house2.Owners.Add(simon);
            simon.OwnedHouses.Add(house2);
            house2.Owners.Add(petra);
            petra.OwnedHouses.Add(house2);

            //Saving entities
            using (var session = NHConfig.OpenSession())
            {
                session.Save(petra);
                session.Save(rok);
                session.Save(simon);
                session.Save(ana);
                session.Flush();
            }
        }