public void AllowModifications_Ref()
        {
            Blog first = new Blog(), second = new Blog();
            blog = new EntityRef<Blog>(
                delegate(Blog b)
                {
                    using (blog.AllowModifications)
                    {
                        blog.Value = second;
                    }
                }, null);
            blog.Value = first;

            Assert.AreSame(second, blog.Value);
        }
        public void UseCascades()
        {
            Blog blog = new Blog("Ayende @ Blog");
            for (int i = 0; i < 59; i++)
            {
                Post post = new Post(i.ToString());
                blog.Posts.Add(post);
            }

            session.Save(blog);

            session.Dispose();

            session = factory.OpenSession();

            Blog fromDb = (Blog)session.Load(typeof(Blog), blog.BlogID);
            Assert.AreEqual(59, fromDb.Posts.Count);
        }
        public void AddBlogToPostAndSave()
        {
            Blog blog = new Blog("My Blog");
            //Need to save it first, so it would exist in the DB
            //when saving the post
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Blog blogFromDB = (Blog)session.Load(typeof(Blog), blog.BlogID);
            Post postFromDB = (Post)session.Load(typeof(Post), post.PostId);

            Assert.AreEqual(blogFromDB, postFromDB.Blog);
            Assert.IsTrue(blogFromDB.Posts.Contains(postFromDB));
        }
        public void Setup()
        {
            blog = new Blog();
            post1 = new Post();
            post2 = new Post();
            post1.Blog = blog;
            post2.Blog = blog;

            session = factory.OpenSession();
            session.Save(blog);
            session.Save(post1);
            session.Save(post2);
            session.Dispose();

            session = factory.OpenSession();
        }
 private static void AssertPostCollectionIsLazy(Blog fromDb)
 {
     Assert.IsFalse(
         NHibernateUtil.IsInitialized(((IWrapper)fromDb.Posts).Value),
         "Lazy loading doesn't work??"
     );
 }
 public void LazyLoadingWhenAddingAPost()
 {
     Blog blog = new Blog("My Blog");
     //Need to save it first, so it would exist in the DB
     //when saving the post
     session.Save(blog);
     session.Flush();
     Post post = new Post("First Post");
     blog.Posts.Add(post);
     session.Save(post);
     session.Dispose();
     session = factory.OpenSession();
     Blog fromDb = (Blog)session.Load(typeof(Blog), blog.BlogID);
     AssertPostCollectionIsLazy(fromDb);
     Post newPost = new Post("Second Post");
     fromDb.Posts.Add(newPost);
     session.Save(newPost);
     AssertPostCollectionIsLazy(fromDb);
 }
        public void LoadCollectionAndThenAddingToItAddsToInMemoryCollection()
        {
            Blog blog = new Blog("My Blog");
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Blog blogFromDB = (Blog)session.Load(typeof(Blog), blog.BlogID);
            EntitySet<Post> posts = (EntitySet<Post>)blogFromDB.Posts;
            posts.Load();//Load the lazy collection.
            Assert.IsTrue(posts.IsInitialized);
            Post newPost = new Post("Second Post");
            blogFromDB.Posts.Add(newPost);
            Assert.IsTrue(blogFromDB.Posts.Contains(newPost));
            Assert.IsTrue(posts.IsInitialized);
        }
        public void CantGetAddedItemsFromLazyLoadCollectionIfNotSavedToDB()
        {
            Blog blog = new Blog("My Blog");
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Blog blogFromDB = (Blog)session.Load(typeof(Blog), blog.BlogID);
            EntitySet<Post> posts = (EntitySet<Post>)blogFromDB.Posts;
            Post newPost = new Post("Second Post");
            blogFromDB.Posts.Add(newPost);
            Assert.IsFalse(posts.IsInitialized);
            Assert.IsFalse(blogFromDB.Posts.Contains(newPost));
            Assert.IsTrue(posts.IsInitialized);
        }
        public void AssignBlogToPostPersistWhilePostsIsNotLoadedDoesNotLoad()
        {
            Blog blog = new Blog("My Blog");
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Blog blogFromDB = (Blog)session.Load(typeof(Blog), blog.BlogID);
            EntitySet<Post> posts = (EntitySet<Post>)blogFromDB.Posts;
            Assert.IsFalse(posts.IsInitialized);
            Post newPost = new Post("Second Post");
            newPost.Blog = blogFromDB;
            Assert.IsFalse(posts.IsInitialized);
        }
        public void AddToPostsDoesNotLoadsThem()
        {
            Blog blog = new Blog("My Blog");
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Blog blogFromDB = (Blog)session.Load(typeof(Blog), blog.BlogID);
            EntitySet<Post> posts = (EntitySet<Post>)blogFromDB.Posts;
            Assert.IsFalse(posts.IsInitialized);
            Post newPost = new Post("Second Post");
            blogFromDB.Posts.Add(newPost);
            Assert.IsFalse(posts.IsInitialized);

            //Need to save before changes will appear
            session.Save(newPost);
            session.Flush();
            Assert.IsTrue(blogFromDB.Posts.Contains(newPost));
        }
        public void AddCommentToPost_SaveAndLoad()
        {
            Blog blog = new Blog("My Blog");
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            post.Comments.Add(new Comment("First Comment"));
            post.Comments.Add(new Comment("Second Comment"));
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Post fromDb = (Post)session.Load(typeof(Post), post.PostId);
            Assert.AreEqual(2, fromDb.Comments.Count);

            Assert.AreEqual("First Comment", fromDb.Comments[0].Text);
            Assert.AreEqual("Second Comment", fromDb.Comments[1].Text);
        }
        public void Setup()
        {
            session = factory.OpenSession();

            ayende = new User("Ayende");
            rahien = new User("Rahien");
            tech = new Blog("Techincal");
            fun = new Blog("Funny Stuff");

            ayende.Blogs.Add(tech);
            ayende.Blogs.Add(fun);

            rahien.Blogs.Add(fun);
            rahien.Blogs.Add(tech);
        }