Exemplo n.º 1
0
        private static void CascadingInsert(BlogDbContext db)
        {
            var post = new Post()
            {
                Title = "New Title 2",
                Body = "New Body.",
                User = db.Users.First(),
                Comments = new Comment[]
                            {
                    new Comment()
                    {
                        Text = "First comment.", Date = DateTime.Now
                    },
                    new Comment()
                    {
                        Text = "Second comment.", Date = DateTime.Now, User = db.Users.First()
                    }
                            },
                Tags = db.Tags.Take(3).ToList(),
                Date = DateTime.Now
            };

            db.Posts.Add(post);
            db.SaveChanges();
        }
Exemplo n.º 2
0
        private static void Delete(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($"Post #{lastPost.ID} removed.");
        }
Exemplo n.º 3
0
        private static void AddPost(BlogDbContext db)
        {
            var post = new Post()
            {
                Title = "New Title",
                Body = "New Body.",
                Date = DateTime.Now
            };

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

            Console.WriteLine($"Post #{post.ID} created successfully.");
        }
Exemplo n.º 4
0
        private static void ExecuteNativeSQL(BlogDbContext db)
        {
            var startDate = new DateTime(2016, 05, 19);
            var endDate = new DateTime(2016, 06, 14);

            var posts = db.Database.SqlQuery<PostData>(
                @"SELECT ID, Title, Date FROM Posts WHERE CONVERT(date, Date) BETWEEN {0} AND {1} ORDER BY Date",
                startDate,
                endDate);

            foreach (var post in posts)
            {
                Console.WriteLine($"#{post.ID}: {post.Title} ({post.Date})");
            }
        }
Exemplo n.º 5
0
        private static void Update(BlogDbContext db)
        {
            var user = db.Users.Where(u => u.Username.Equals("Mariika")).First();

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

            Console.WriteLine($"User #{user.ID} ({user.Username}) has a new random password.");
        }
Exemplo n.º 6
0
        static void Main()
        {
            var db = new BlogDbContext();

            //ListUsers(db);

            //ListPosts(db);

            //AddPost(db);

            //CascadingInsert(db);

            //Update(db);

            //Delete(db);

            //ExecuteNativeSQL(db);
        }
Exemplo n.º 7
0
 private static void ListUsers(BlogDbContext db)
 {
     foreach (User user in db.Users)
     {
         Console.WriteLine(user.Username);
     }
 }
Exemplo n.º 8
0
        private static void ListPosts(BlogDbContext db)
        {
            var posts = db.Posts.Select(p => new
            {
                p.ID,
                p.Title,
                Comments = p.Comments.Count(),
                Tags = p.Tags.Count()
            });

            Console.WriteLine($"SQL query:\n{posts}\n");

            foreach (var post in posts)
            {
                Console.WriteLine($"#{post.ID} {post.Title}: Comments - {post.Comments}, Tags: {post.Tags}");
            }
        }