private void button1_Click_1(object sender, EventArgs e)
        {
            var selectedBlog = (string)comboBox1.SelectedItem;
            int blogId;

            //check validation
            using (var ctx = new BlogContext())    {
                blogId = (from b in ctx.Blogs where b.Name == selectedBlog select b.BlogId).FirstOrDefault();
                if (blogId == 0)    {
                    MessageBox.Show("You've not selected any blogs.");
                    return;
                }
            }
            if (textBox1.Text == "")    {
                MessageBox.Show("There is no title");
                return;
            }
            if (richTextBox1.Text == "")    {
                MessageBox.Show("No content");
                return;
            }
            var post = new Post();
            post.BlogId = blogId;
            post.Content = richTextBox1.Text;
            post.Title = textBox1.Text;
            //add and save post
            using (var ctx = new BlogContext())    {
                ctx.Posts.Add(post);
                ctx.SaveChanges();
            }
            Hide();
            DestroyHandle();
        }
 private void BlogForm_Load(object sender, EventArgs e)
 {
     bContext = new BlogContext();
     bContext.Blogs.Load();
     bContext.Posts.Load();
     this.blogBindingSource.DataSource = bContext.Blogs.Local.ToBindingList();
     this.postsBindingSource.DataSource = bContext.Posts.Local.ToBindingList();
 }
 static void countPosts(BlogContext bc)
 {
     var query = from post in bc.Posts
                 group post by post.BlogId into BlogGroup
                 select new { BlogID = BlogGroup.Key, PostCount = BlogGroup.Count() };
     foreach (var b in query)
     {
         Console.WriteLine("BlogID: {0} PostCount: {1}", b.BlogID, b.PostCount );
     }
 }
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     using (var ctx = new BlogContext())    {
         label2.Text = (from b in ctx.Blogs
                        from p in ctx.Posts
                        where (b.BlogId == p.BlogId && b.Name == comboBox1.SelectedItem)
                        select b.Posts).ToList().Count().ToString();
         label2.Update();
     }
 }
 static void listBlogsAndPostsNav(BlogContext bc)
 {
     var blogs = bc.Blogs.Include(b => b.Posts).ToList();
     foreach (var b in blogs)
     {
         Console.WriteLine("BlogID: {0} BlogName: {1} Posts: ", b.BlogId, b.Name);
         foreach (var p in b.Posts)
         {
             Console.WriteLine("title: {0} id: {1}", p.Title, p.PostId);
         }
     }
 }
        private void StatisticsForm_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("Blogs to choose:");
            comboBox1.SelectedItem = comboBox1.Items[0];

            using (var ctx = new BlogContext()) {
                var blogs = ctx.Blogs.Select(s => s.Name);
                foreach (var blog in blogs) {
                    if (blog!= null)
                        comboBox1.Items.Add(blog);
                }
            }
        }
        private void AddPostForm_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("Blogs to choose:");

            using (var context = new BlogContext()) {
                var blogs = from blog in context.Blogs
                            orderby blog.Name
                            select blog.Name;

                foreach (var blog in blogs) {
                    if (blog != null)
                        comboBox1.Items.Add(blog);
                }
            }
            comboBox1.SelectedItem = comboBox1.Items[0];
        }
        static void listBlogsAndPostsQuery(BlogContext blogContext)
        {
            DbSet<Blog> blogs = blogContext.Blogs;
            DbSet<Post> posts = blogContext.Posts;

            var query = from blog in blogs
                        join post in posts
                        on blog.BlogId equals post.BlogId
                        select new
                        {
                            BlogID = blog.BlogId,
                            BlogName = blog.Name,
                            PostTitle = post.Title
                        };
            foreach (var postItem in query)
            {
                Console.WriteLine("BlogID: {0} BlogName: {1} Post: {2}", postItem.BlogID, postItem.BlogName, postItem.PostTitle);
            }
        }
 static void listBlogsAndPostsNavQuery(BlogContext bc)
 {
     var blogs = from b in bc.Blogs
                 select new
                     {
                         BlogName = b.Name,
                         BlogID = b.BlogId,
                         Posts = b.Posts
                     };
         //bc.Blogs.Include(b => b.Posts).ToList();
     foreach (var b in blogs)
     {
         Console.WriteLine("BlogID: {0} BlogName: {1} Posts: ", b.BlogID, b.BlogName);
         foreach (var p in b.Posts)
         {
             Console.WriteLine("title: {0} id: {1}", p.Title, p.PostId);
         }
     }
 }
        static void listBlogsAndPosts(BlogContext blogContext)
        {
            DbSet<Blog> blogs = blogContext.Blogs;
            DbSet<Post> posts = blogContext.Posts;

            var query = blogs.Join(
                posts,
                post => post.BlogId,
                blog => blog.BlogId,
                (blog, post) => new
                {
                    BlogID = blog.BlogId,
                    BlogName = blog.Name,
                    PostTitle = post.Title
                });
            foreach (var postItem in query)
            {
                Console.WriteLine("BlogID: {0} BlogName: {1} Post: {2}", postItem.BlogID, postItem.BlogName, postItem.PostTitle);
            }
        }
 static void printBlogs(BlogContext blogContext)
 {
     Console.WriteLine("Blogi w projekcie: ");
     IQueryable<string> blogNames = blogContext.Blogs.Select(b => b.Name).OrderByDescending(name => name);
     foreach (String name in blogNames)
     {
         Console.WriteLine(name);
     }
 }
 static void printBlogImm(BlogContext blogContext)
 {
     Console.WriteLine("Blogi w projekcie: ");
     String[] blogNames = blogContext.Blogs.Select(b => b.Name).OrderByDescending(name => name).ToArray();
     foreach (String name in blogNames)
     {
         Console.WriteLine(name);
     }
 }
        static void Main(string[] args)
        {
            Console.WriteLine("Podaj nazwę bloga: ");
            String blogName = Console.ReadLine();
            Blog firstBlog = new Blog { Name = blogName };
            BlogContext blogContext = new BlogContext();
            blogContext.Blogs.Add(firstBlog);
            blogContext.SaveChanges();

            printBlogs(blogContext);
            //listBlogsAndPosts(blogContext);
            //listBlogsAndPostsNav(blogContext);
            listBlogsAndPostsNavQuery(blogContext);
            countPosts(blogContext);

            BlogForm form = new BlogForm();
            form.ShowDialog();

            Console.ReadLine();
        }