public MapScenario(ISessionFactory factory) { this.factory = factory; using (ISession s = factory.OpenSession()) { using (ITransaction t = s.BeginTransaction()) { var entity = new Base(); entity.NamedChildren = new Dictionary<string, Child> { {"Child1", new Child()}, {"NullChild", null}, }; var child1 = new AnotherChild { Name = "AnotherChild1" }; var child2 = new AnotherChild { Name = "AnotherChild2" }; s.Save(child1); s.Save(child2); entity.OneToManyNamedChildren = new Dictionary<string, AnotherChild> { {"AnotherChild1" , child1}, {"AnotherChild2" , child2} }; s.Save(entity); t.Commit(); } } }
public MapScenario(ISessionFactory factory) { this.factory = factory; using (ISession s = factory.OpenSession()) { using (ITransaction t = s.BeginTransaction()) { var entity = new Base(); entity.NamedChildren = new Dictionary <string, Child> { { "Child1", new Child() }, { "NullChild", null }, }; var child1 = new AnotherChild { Name = "AnotherChild1" }; var child2 = new AnotherChild { Name = "AnotherChild2" }; s.Save(child1); s.Save(child2); entity.OneToManyNamedChildren = new Dictionary <string, AnotherChild> { { "AnotherChild1", child1 }, { "AnotherChild2", child2 } }; s.Save(entity); t.Commit(); } } }
public void Map_Item() { using (new MapScenario(Sfi)) { using (ISession s = OpenSession()) { using (ITransaction t = s.BeginTransaction()) { // accessing an invalid key should fail or throw an exception, depending on method var entity = s.CreateQuery("from Base").UniqueResult <Base>(); // null collection members don't seem to work, at least for lazy="extra" collections entity.NamedChildren.Count.Should().Be.EqualTo(2); entity.OneToManyNamedChildren.Count.Should().Be.EqualTo(2); NHibernateUtil.IsInitialized(entity.NamedChildren).Should().Be.False(); Executing.This(() => { Child ignored = entity.NamedChildren["InvalidKey"]; }).Should().Throw <KeyNotFoundException>(); Executing.This(() => { AnotherChild ignored = entity.OneToManyNamedChildren["InvalidKey"]; }).Should().Throw <KeyNotFoundException>(); NHibernateUtil.IsInitialized(entity.NamedChildren).Should().Be.False(); } } } }
public async Task Map_ItemAsync() { using (new MapScenario(Sfi)) { using (ISession s = OpenSession()) { using (ITransaction t = s.BeginTransaction()) { // accessing an invalid key should fail or throw an exception, depending on method var entity = await(s.CreateQuery("from Base").UniqueResultAsync <Base>()); // null collection members don't seem to work, at least for lazy="extra" collections Assert.That(entity.NamedChildren.Count, Is.EqualTo(2)); Assert.That(entity.OneToManyNamedChildren.Count, Is.EqualTo(2)); Assert.That(NHibernateUtil.IsInitialized(entity.NamedChildren), Is.False); Assert.That(() => { Child ignored = entity.NamedChildren["InvalidKey"]; }, Throws.TypeOf <KeyNotFoundException>()); Assert.That(() => { AnotherChild ignored = entity.OneToManyNamedChildren["InvalidKey"]; }, Throws.TypeOf <KeyNotFoundException>()); Assert.That(NHibernateUtil.IsInitialized(entity.NamedChildren), Is.False); } } } }