-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
50 lines (43 loc) · 1.6 KB
/
Program.cs
File metadata and controls
50 lines (43 loc) · 1.6 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
using System;
using System.Linq;
namespace TestingCodeFirst_FirstProject
{
internal class Program
{
static void Main(string[] args)
{
using(var db = new DBContext.BloggingContext())
{
// Ensure the database is created
db.Database.CreateIfNotExists();
{
// Create a new blog
Models.Blog blog = new Models.Blog { Name = "My First Blog" };
db.Blogs.Add(blog);
// Save changes to the database
db.SaveChanges();
{
// Create a new post
var post = new Models.Post { Title = "Hello World", Content = "This is my first post!", BlogId = blog.BlogId };
db.Posts.Add(post);
// Save changes to the database
db.SaveChanges();
}
}
{
// Retrieve and display the blog and its posts
//var blogs = db.Blogs.Include(b => b.Posts).ToList();
var blogs = db.Blogs.Include("Posts").ToList(); // Fixed: Use string-based Include for EF6
foreach (var b in blogs)
{
Console.WriteLine($"Blog: {b.Name}");
foreach (var p in b.Posts)
{
Console.WriteLine($" - Post: {p.Title}");
}
}
}
}
}
}
}