コード例 #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();
            }
        }
コード例 #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://sample.com"
                };
                context.Blogs.Add(blog);
                context.SaveChanges();
            }
            #endregion

            #region Update
            using (var context = new BloggingContext())
            {
                var blog = context.Blogs.First();
                blog.Url = "http://sample.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://sample.com/blog"
                });
                context.Blogs.Add(new Blog {
                    Url = "http://sample.com/another_blog"
                });
                context.SaveChanges();
            }

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

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

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

                context.SaveChanges();
            }
            #endregion
        }
コード例 #3
0
ファイル: Sample.cs プロジェクト: xmzhan/EntityFramework.Docs
        public static async Task RunAsync()
        {
            await using (var context = new BloggingContext())
            {
                await context.Database.EnsureDeletedAsync();

                await context.Database.EnsureCreatedAsync();
            }

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

            #region Update
            await using (var context = new BloggingContext())
            {
                var blog = await context.Blogs.FirstAsync();

                blog.Url = "http://example.com/blog";
                await context.SaveChangesAsync();
            }
            #endregion

            #region Remove
            await using (var context = new BloggingContext())
            {
                var blog = await context.Blogs.FirstAsync();

                context.Blogs.Remove(blog);
                await context.SaveChangesAsync();
            }
            #endregion

            #region MultipleOperations
            await 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"
                });
                await context.SaveChangesAsync();
            }

            await 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 = await context.Blogs.FirstAsync();

                firstBlog.Url = "";

                // remove
                var lastBlog = await context.Blogs.LastAsync();

                context.Blogs.Remove(lastBlog);

                await context.SaveChangesAsync();
            }
            #endregion
        }
コード例 #4
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();
            }
        }