Exemplo n.º 1
0
        private static void CreateNewData(BlogDBContext db)
        {
            var post = new Post()
            {
                Title    = "New Title",
                Body     = "This post have comments and tags",
                Date     = DateTime.Now,
                User     = db.Users.First(),
                Comments =
                    new List <Comment>
                {
                    new Comment()
                    {
                        Text = "Comment 1", Date = DateTime.Now
                    },
                    new Comment()
                    {
                        Text = "Comment 2",
                        Date = DateTime.Now,
                        User = db.Users.First()
                    }
                },
                Tags = db.Tags.Take(3).ToList()
            };

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

            Console.WriteLine($"Post #{post.ID} created.");
        }
Exemplo n.º 2
0
        private static void UpdateExistingData(BlogDBContext db)
        {
            var user = db.Users.First(u => u.Username == "maria");

            user.PasswordHash = Guid.NewGuid().ToByteArray();
            db.SaveChanges();

            Console.WriteLine($"User #{user.ID} ({user.Username}) has a new random password.");
        }
Exemplo n.º 3
0
        private static void DeleteExistingData(BlogDBContext db)
        {
            var lastPost = db.Posts.OrderByDescending(p => p.ID).First();

            db.Comments.RemoveRange(lastPost.Comments);
            lastPost.Tags.Clear();
            db.Posts.Remove(lastPost);
            db.SaveChanges();

            Console.WriteLine($"Deleted post #{lastPost.ID}");
        }