public virtual void AddClass1(Class1 toAdd) { if (false == _class1List.Contains(toAdd)) { _class1List.Add(toAdd); toAdd.AddClass2(this); } }
public void WhenTwoTransactionInSameSessionThenNotChangeVersion() { // the second transaction does not change the entity state Class1 c1_1; Class1 c1_2; Class2 c2_1; Class2 c2_2; int originalVersionC1_1; int originalVersionC1_2; int originalVersionC2_1; int originalVersionC2_2; c1_1 = new Class1(); c1_2 = new Class1(); c2_1 = new Class2(); c2_2 = new Class2(); c1_1.AddClass2(c2_1); c1_2.AddClass2(c2_2); using (ISession s = OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { s.Save(c2_1); s.Save(c1_1); s.Save(c2_2); s.Save(c1_2); tx.Commit(); } originalVersionC1_1 = c1_1.EntityVersion; originalVersionC1_2 = c1_2.EntityVersion; originalVersionC2_1 = c2_1.EntityVersion; originalVersionC2_2 = c2_2.EntityVersion; using (ITransaction tx = s.BeginTransaction()) { s.Refresh(c1_1); // The addition of these two Refresh calls fixes the entity version issue s.Refresh(c1_2); var class1dto = new Class1DTO {ID = c1_1.ID, EntityVersion = c1_1.EntityVersion}; if (c1_1.Class2List.Count > 0) { class1dto.Class2Ary = new Class2DTO[c1_1.Class2List.Count]; for (int i = 0; i < c1_1.Class2List.Count; ++i) { Class2 cl2 = c1_1.Class2List[i]; class1dto.Class2Ary[i] = new Class2DTO {ID = cl2.ID, EntityVersion = cl2.EntityVersion}; } } tx.Commit(); } // After close the second transaction the version was not changed c1_1.EntityVersion.Should().Be(originalVersionC1_1); c1_2.EntityVersion.Should().Be(originalVersionC1_2); c2_1.EntityVersion.Should().Be(originalVersionC2_1); c2_2.EntityVersion.Should().Be(originalVersionC2_2); } // After close the session the version was not changed c1_1.EntityVersion.Should().Be(originalVersionC1_1); c1_2.EntityVersion.Should().Be(originalVersionC1_2); c2_1.EntityVersion.Should().Be(originalVersionC2_1); c2_2.EntityVersion.Should().Be(originalVersionC2_2); using (ISession s = OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { c2_1 = s.Get<Class2>(c2_1.ID); c1_1 = s.Get<Class1>(c1_1.ID); c2_2 = s.Get<Class2>(c2_2.ID); c1_2 = s.Get<Class1>(c1_2.ID); // to be 100% sure the version was not changed in DB c1_1.EntityVersion.Should().Be(originalVersionC1_1); c1_2.EntityVersion.Should().Be(originalVersionC1_2); c2_1.EntityVersion.Should().Be(originalVersionC2_1); c2_2.EntityVersion.Should().Be(originalVersionC2_2); s.Delete(c2_1); s.Delete(c1_1); s.Delete(c2_2); s.Delete(c1_2); tx.Commit(); } } }
public void WhenTwoTransactionInSameSessionThenNotChangeVersion() { // the second transaction does not change the entity state Class1 c1_1; Class1 c1_2; Class2 c2_1; Class2 c2_2; int originalVersionC1_1; int originalVersionC1_2; int originalVersionC2_1; int originalVersionC2_2; c1_1 = new Class1(); c1_2 = new Class1(); c2_1 = new Class2(); c2_2 = new Class2(); c1_1.AddClass2(c2_1); c1_2.AddClass2(c2_2); using (ISession s = OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { s.Save(c2_1); s.Save(c1_1); s.Save(c2_2); s.Save(c1_2); tx.Commit(); } originalVersionC1_1 = c1_1.EntityVersion; originalVersionC1_2 = c1_2.EntityVersion; originalVersionC2_1 = c2_1.EntityVersion; originalVersionC2_2 = c2_2.EntityVersion; using (ITransaction tx = s.BeginTransaction()) { s.Refresh(c1_1); // The addition of these two Refresh calls fixes the entity version issue s.Refresh(c1_2); var class1dto = new Class1DTO { ID = c1_1.ID, EntityVersion = c1_1.EntityVersion }; if (c1_1.Class2List.Count > 0) { class1dto.Class2Ary = new Class2DTO[c1_1.Class2List.Count]; for (int i = 0; i < c1_1.Class2List.Count; ++i) { Class2 cl2 = c1_1.Class2List[i]; class1dto.Class2Ary[i] = new Class2DTO { ID = cl2.ID, EntityVersion = cl2.EntityVersion }; } } tx.Commit(); } // After close the second transaction the version was not changed Assert.That(c1_1.EntityVersion, Is.EqualTo(originalVersionC1_1)); Assert.That(c1_2.EntityVersion, Is.EqualTo(originalVersionC1_2)); Assert.That(c2_1.EntityVersion, Is.EqualTo(originalVersionC2_1)); Assert.That(c2_2.EntityVersion, Is.EqualTo(originalVersionC2_2)); } // After close the session the version was not changed Assert.That(c1_1.EntityVersion, Is.EqualTo(originalVersionC1_1)); Assert.That(c1_2.EntityVersion, Is.EqualTo(originalVersionC1_2)); Assert.That(c2_1.EntityVersion, Is.EqualTo(originalVersionC2_1)); Assert.That(c2_2.EntityVersion, Is.EqualTo(originalVersionC2_2)); using (ISession s = OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { c2_1 = s.Get <Class2>(c2_1.ID); c1_1 = s.Get <Class1>(c1_1.ID); c2_2 = s.Get <Class2>(c2_2.ID); c1_2 = s.Get <Class1>(c1_2.ID); // to be 100% sure the version was not changed in DB Assert.That(c1_1.EntityVersion, Is.EqualTo(originalVersionC1_1)); Assert.That(c1_2.EntityVersion, Is.EqualTo(originalVersionC1_2)); Assert.That(c2_1.EntityVersion, Is.EqualTo(originalVersionC2_1)); Assert.That(c2_2.EntityVersion, Is.EqualTo(originalVersionC2_2)); s.Delete(c2_1); s.Delete(c1_1); s.Delete(c2_2); s.Delete(c1_2); tx.Commit(); } } }