Ejemplo n.º 1
0
        public void MergeManyToManyWithCollectionDeference()
        {
            if (!TestDialect.SupportsEmptyInsertsOrHasNonIdentityNativeGenerator)
            {
                Assert.Ignore("Support of empty inserts is required");
            }

            // setup base data...
            Competition competition;

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    competition = new Competition();
                    competition.Competitors.Add(new Competitor {
                        Name = "Name"
                    });
                    competition.Competitors.Add(new Competitor());
                    competition.Competitors.Add(new Competitor());
                    s.Persist(competition);
                    tx.Commit();
                }

            // the competition graph is now detached:
            //   1) create a new List reference to represent the competitors
            Competition competition2;

            using (var s = OpenSession())
                using (var tx = s.BeginTransaction())
                {
                    var        newComp            = new List <Competitor>();
                    Competitor originalCompetitor = competition.Competitors[0];
                    originalCompetitor.Name = "Name2";
                    newComp.Add(originalCompetitor);
                    newComp.Add(new Competitor());
                    //   2) set that new List reference unto the Competition reference
                    competition.Competitors = newComp;
                    //   3) attempt the merge
                    competition2 = (Competition)s.Merge(competition);
                    tx.Commit();
                }

            Assert.That(!(competition == competition2));
            Assert.That(!(competition.Competitors == competition2.Competitors));
            Assert.That(competition2.Competitors.Count, Is.EqualTo(2));

            using (var s = OpenSession())
                using (var tx = s.BeginTransaction())
                {
                    competition = s.Get <Competition>(competition.Id);
                    Assert.That(competition.Competitors.Count, Is.EqualTo(2));
                    s.Delete(competition);
                    tx.Commit();
                }
        }
Ejemplo n.º 2
0
        public async Task MergeManyToManyWithCollectionDeferenceAsync()
        {
            // setup base data...
            Competition competition;

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    competition = new Competition();
                    competition.Competitors.Add(new Competitor {
                        Name = "Name"
                    });
                    competition.Competitors.Add(new Competitor());
                    competition.Competitors.Add(new Competitor());
                    await(s.PersistAsync(competition));
                    await(tx.CommitAsync());
                }

            // the competition graph is now detached:
            //   1) create a new List reference to represent the competitors
            Competition competition2;

            using (var s = OpenSession())
                using (var tx = s.BeginTransaction())
                {
                    var        newComp            = new List <Competitor>();
                    Competitor originalCompetitor = competition.Competitors[0];
                    originalCompetitor.Name = "Name2";
                    newComp.Add(originalCompetitor);
                    newComp.Add(new Competitor());
                    //   2) set that new List reference unto the Competition reference
                    competition.Competitors = newComp;
                    //   3) attempt the merge
                    competition2 = (Competition)await(s.MergeAsync(competition));
                    await(tx.CommitAsync());
                }

            Assert.That(!(competition == competition2));
            Assert.That(!(competition.Competitors == competition2.Competitors));
            Assert.That(competition2.Competitors.Count, Is.EqualTo(2));

            using (var s = OpenSession())
                using (var tx = s.BeginTransaction())
                {
                    competition = await(s.GetAsync <Competition>(competition.Id));
                    Assert.That(competition.Competitors.Count, Is.EqualTo(2));
                    await(s.DeleteAsync(competition));
                    await(tx.CommitAsync());
                }
        }
Ejemplo n.º 3
0
        public void MergeManyToManyWithCollectionDeference()
        {
            // setup base data...
            ISession     s           = OpenSession();
            ITransaction tx          = s.BeginTransaction();
            var          competition = new Competition();

            competition.Competitors.Add(new Competitor {
                Name = "Name"
            });
            competition.Competitors.Add(new Competitor());
            competition.Competitors.Add(new Competitor());
            s.Persist(competition);
            tx.Commit();
            s.Close();

            // the competition graph is now detached:
            //   1) create a new List reference to represent the competitors
            s  = OpenSession();
            tx = s.BeginTransaction();
            var        newComp            = new List <Competitor>();
            Competitor originalCompetitor = competition.Competitors[0];

            originalCompetitor.Name = "Name2";
            newComp.Add(originalCompetitor);
            newComp.Add(new Competitor());
            //   2) set that new List reference unto the Competition reference
            competition.Competitors = newComp;
            //   3) attempt the merge
            var competition2 = (Competition)s.Merge(competition);

            tx.Commit();
            s.Close();

            Assert.That(!(competition == competition2));
            Assert.That(!(competition.Competitors == competition2.Competitors));
            Assert.That(competition2.Competitors.Count, Is.EqualTo(2));

            s           = OpenSession();
            tx          = s.BeginTransaction();
            competition = s.Get <Competition>(competition.Id);
            Assert.That(competition.Competitors.Count, Is.EqualTo(2));
            s.Delete(competition);
            tx.Commit();
            s.Close();
        }