Exemplo n.º 1
0
        public static void Run()
        {
            using (var db = new BloggingContext())
            {
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();
            }

            using (var db = new BloggingContext())
            {
                var blog = new Blog { Url = "http://sample.com" };
                db.Blogs.Add(blog);
                db.SaveChanges();

                Console.WriteLine(blog.BlogId + ": " +  blog.Url);
            }

            using (var db = new BloggingContext())
            {
                var blog = db.Blogs.First();
                blog.Url = "http://sample.com/blog";
                db.SaveChanges();
            }

            using (var db = new BloggingContext())
            {
                var blog = db.Blogs.First();
                db.Blogs.Remove(blog);
                db.SaveChanges();
            }

            // Insert some seed data for the final example
            using (var db = new BloggingContext())
            {
                db.Blogs.Add(new Blog { Url = "http://sample.com/blog" });
                db.Blogs.Add(new Blog { Url = "http://sample.com/another_blog" });
                db.SaveChanges();
            }

            using (var db = new BloggingContext())
            {
                db.Blogs.Add(new Blog { Url = "http://sample.com/blog_one" });
                db.Blogs.Add(new Blog { Url = "http://sample.com/blog_two" });

                var firstBlog = db.Blogs.First();
                firstBlog.Url = "";

                var lastBlog = db.Blogs.Last();
                db.Blogs.Remove(lastBlog);

                db.SaveChanges();
            }
        }
Exemplo n.º 2
0
        public static void Run()
        {
            using (var context = new BloggingContext())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
            }

            #region Add
            using (var context = new BloggingContext())
            {
                var blog = new Blog {
                    Url = "http://example.com"
                };
                context.Blogs.Add(blog);
                context.SaveChanges();
            }
            #endregion

            #region Update
            using (var context = new BloggingContext())
            {
                var blog = context.Blogs.First();
                blog.Url = "http://example.com/blog";
                context.SaveChanges();
            }
            #endregion

            #region Remove
            using (var context = new BloggingContext())
            {
                var blog = context.Blogs.First();
                context.Blogs.Remove(blog);
                context.SaveChanges();
            }
            #endregion

            #region MultipleOperations
            using (var context = new BloggingContext())
            {
                // seeding database
                context.Blogs.Add(new Blog {
                    Url = "http://example.com/blog"
                });
                context.Blogs.Add(new Blog {
                    Url = "http://example.com/another_blog"
                });
                context.SaveChanges();
            }

            using (var context = new BloggingContext())
            {
                // add
                context.Blogs.Add(new Blog {
                    Url = "http://example.com/blog_one"
                });
                context.Blogs.Add(new Blog {
                    Url = "http://example.com/blog_two"
                });

                // update
                var firstBlog = context.Blogs.First();
                firstBlog.Url = "";

                // remove
                var lastBlog = context.Blogs.Last();
                context.Blogs.Remove(lastBlog);

                context.SaveChanges();
            }
            #endregion
        }
Exemplo n.º 3
0
        public static void Run()
        {
            using (var db = new BloggingContext())
            {
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();
            }

            using (var db = new BloggingContext())
            {
                var blog = new Blog {
                    Url = "http://sample.com"
                };
                db.Blogs.Add(blog);
                db.SaveChanges();

                Console.WriteLine(blog.BlogId + ": " + blog.Url);
            }

            using (var db = new BloggingContext())
            {
                var blog = db.Blogs.First();
                blog.Url = "http://sample.com/blog";
                db.SaveChanges();
            }

            using (var db = new BloggingContext())
            {
                var blog = db.Blogs.First();
                db.Blogs.Remove(blog);
                db.SaveChanges();
            }

            // Insert some seed data for the final example
            using (var db = new BloggingContext())
            {
                db.Blogs.Add(new Blog {
                    Url = "http://sample.com/blog"
                });
                db.Blogs.Add(new Blog {
                    Url = "http://sample.com/another_blog"
                });
                db.SaveChanges();
            }

            using (var db = new BloggingContext())
            {
                db.Blogs.Add(new Blog {
                    Url = "http://sample.com/blog_one"
                });
                db.Blogs.Add(new Blog {
                    Url = "http://sample.com/blog_two"
                });

                var firstBlog = db.Blogs.First();
                firstBlog.Url = "";

                var lastBlog = db.Blogs.First();
                db.Blogs.Remove(lastBlog);

                db.SaveChanges();
            }
        }