示例#1
0
        public static void Main(string[] args)
        {
            var context = new BloggingContext();

            WriteLine("---------------------------------------------------");
            WriteLine("Entity frameworks Core 1.0 Demos");
            WriteLine("---------------------------------------------------");
            WriteLine($"{context.Posts.CountAsync().Result} Posts found.");
            WriteLine($"{context.Blogs.CountAsync().Result} Blogs found.");

            WriteLine("Removing All DB entries.");
            foreach(var post in context.Posts)
            {
                context.Posts.Remove(post);
            }
            foreach(var blog in context.Blogs)
            {
                context.Blogs.Remove(blog);
            }
            context.SaveChanges();

            WriteLine($"{context.Posts.CountAsync().Result} Posts found.");
            WriteLine($"{context.Blogs.CountAsync().Result} Blogs found.");
            WriteLine("---------------------------------------------------");

            WriteLine("Creating Data...");
            for(int blogIndex = 0; blogIndex < NumberOfBlogs; blogIndex++)
            {
                var blog = new Blog() 
                { 
                    Url = $"http://mysite/{blogIndex}/blog",
                    Name = $"Blog #{blogIndex + 1}"
                };
                context.Blogs.Add(blog);

                for(int postIndex = 0; postIndex < NumberOfPostsPerBlog; postIndex++)
                {
                    var post = new Post
                    {
                        Title = $"Post #{postIndex + 1} Blog #{blogIndex + 1}",
                        Content = "Lorem ipsum delorum est",
                        Blog = blog 
                    };
                    context.Posts.Add(post);
                }
            }
            context.SaveChanges();
            WriteLine("Created Data.");

            WriteLine("---------------------------------------------------");
            WriteLine($"{context.Posts.CountAsync().Result} Posts found.");
            WriteLine($"{context.Blogs.CountAsync().Result} Blogs found.");
            WriteLine("---------------------------------------------------");
            WriteLine("Complete.");
        }
        public static void Main(string[] args)
        {
            using (var db = new BloggingContext())
            {
                db.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
                var count = db.SaveChanges();
                Console.WriteLine("{0} records saved to database", count);

                Console.WriteLine();
                Console.WriteLine("All blogs in database:");
                foreach (var blog in db.Blogs)
                {
                    Console.WriteLine(" - {0}", blog.Url);
                }
            }
        }