-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathProgram.cs
More file actions
94 lines (79 loc) · 3 KB
/
Copy pathProgram.cs
File metadata and controls
94 lines (79 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using EFTutorial.Models;
using System.Linq;
namespace EFTutorial
{
class Program
{
static void Main(string[] args)
{
// 1. List Posts for Blog #1
using (var db = new BlogContext())
{
var blog = db.Blogs.Where(x=>x.BlogId == 1).FirstOrDefault();
// var blogsList = blog.ToList(); // convert to List from IQueryable
System.Console.WriteLine($"Posts for Blog {blog.Name}");
foreach (var post in blog.Posts) {
System.Console.WriteLine($"\tPost {post.PostId} {post.Title}");
}
}
// 2. Add Post to database
// System.Console.WriteLine("Enter your Post title");
// var postTitle = Console.ReadLine();
// var post = new Post();
// post.Title = postTitle;
// post.BlogId = 1;
// using (var db = new BlogContext())
// {
// db.Posts.Add(post);
// db.SaveChanges();
// }
// 3. Read Blogs from database
// using (var db = new BlogContext())
// {
// System.Console.WriteLine("Here is the list of blogs");
// foreach (var b in db.Blogs) {
// System.Console.WriteLine($"Blog: {b.BlogId}: {b.Name}");
// }
// }
// 4. Add Blog to Database
// System.Console.WriteLine("Enter your Blog name");
// var blogName = Console.ReadLine();
// // Create new Blog
// var blog = new Blog();
// blog.Name = blogName;
// // Save blog object to database
// using (var db = new BlogContext())
// {
// db.Add(blog);
// db.SaveChanges();
// }
// 5. List Posts in Blog using Eager Loading
// using (var db = new BlogContext())
// {
// var blogs = db.Blogs.Include(blog => blog.Posts);
// foreach (var blog in blogs)
// {
// Console.WriteLine($"Blog: {blog.BlogId}: {blog.Name}");
// foreach (var post in blog.Posts)
// {
// Console.WriteLine($" Post: {post.PostId}: {post.Title}");
// }
// }
// }
// 6. List Posts in Blog using Lazy Loading
// using (var db = new BlogContext())
// {
// var blogs = db.Blogs.ToList();
// foreach (var blog in blogs)
// {
// Console.WriteLine($"Blog: {blog.BlogId}: {blog.Name}");
// foreach (var post in blog.Posts)
// {
// Console.WriteLine($" Post: {post.PostId}: {post.Title}");
// }
// }
// }
}
}
}