Beispiel #1
0
        public void SeedBlogs()
        {
            var bob = new Blog {
                Title = "Bog's blog", Description = "This is bob's blog", AuthorId = "Bob", AuthorEmail = "bob@us", AuthorName = "Mr. Bob"
            };
            var sam = new Blog {
                Title = "Sam's blog", Description = "This is sam's blog", AuthorId = "Sam", AuthorEmail = "sam@us", AuthorName = "Mr. Sam"
            };

            using (var db = new BlogiFireContext())
            {
                if (db.Blogs.Where(b => b.AuthorId == "Bob").FirstOrDefault() == null)
                {
                    var b1 = db.Blogs.Add(bob);
                    db.SaveChanges();
                    SeedPosts(b1);
                }
                if (db.Blogs.Where(b => b.AuthorId == "Sam").FirstOrDefault() == null)
                {
                    var b2 = db.Blogs.Add(sam);
                    db.SaveChanges();
                    SeedPosts(b2);
                }
            }
        }
Beispiel #2
0
        public void SeedPosts(Blog blog)
        {
            using (var db = new BlogiFireContext())
            {
                for (int i = 1; i <= 25; i++)
                {
                    var post        = new Post();
                    var blogContent = GetContent();

                    post.BlogId     = blog.Id;
                    post.AuthorName = blog.AuthorName;
                    post.Title      = string.Format("Post title{0} from {1}", i, blog.AuthorId);
                    post.Slug       = string.Format("post-title{0}-{1}", i, blog.AuthorId);
                    post.Content    = string.Format("This is content of the post {0} written by {1}. {2}", i, blog.AuthorName, blogContent);
                    post.Published  = DateTime.UtcNow.AddHours((i - 26) * 5 * blog.Id);
                    post.Saved      = post.Published;

                    db.Posts.Add(post);
                    db.SaveChanges();

                    // add comments to every third post
                    if (i % 3 == 0)
                    {
                        var comments = GetComments();
                        foreach (var com in comments)
                        {
                            var c = new Comment
                            {
                                Author     = "visitor" + i.ToString(),
                                Email      = "visitor" + i.ToString() + "@us.com",
                                Content    = com,
                                PostId     = post.Id,
                                IsApproved = true,
                                Published  = DateTime.UtcNow.AddHours((i - 15) * 5 * blog.Id)
                            };
                            db.Comments.Add(c);
                            post.Comments++;
                        }
                        db.SaveChanges();
                    }
                }
            }
        }