Ejemplo n.º 1
0
		public void LazyLoad_Initialize_AndEvict()
		{
			Category category = new Category("parent");
			category.AddSubcategory(new Category("child"));
			SaveCategory(category);

			using (ISession session = OpenSession())
			using (ITransaction transaction = session.BeginTransaction())
			{
				Category loaded = session.Load<Category>(category.Id);
				NHibernateUtil.Initialize(loaded.Subcategories[0]);
				session.Evict(loaded);
				transaction.Commit();
				Assert.AreEqual("child", loaded.Subcategories[0].Name, "cannot access child");
			}
			using (ISession session = OpenSession())
			using (ITransaction transaction = session.BeginTransaction())
			{
				// first delete children
				session.CreateQuery("delete from Category where Parent != null").ExecuteUpdate();
				// then the rest
				session.CreateQuery("delete from Category").ExecuteUpdate();
				transaction.Commit();
			}
		}
Ejemplo n.º 2
0
		private void SaveCategory(Category category)
		{
			using (ISession session = OpenSession())
			using (ITransaction transaction = session.BeginTransaction())
			{
				session.SaveOrUpdate(category);
				transaction.Commit();
			}
		}
Ejemplo n.º 3
0
		public virtual void AddSubcategory(Category subcategory)
		{
			subcategories.Add(subcategory);
			subcategory.Parent = this;
		}