-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
48 lines (40 loc) · 1.45 KB
/
Program.cs
File metadata and controls
48 lines (40 loc) · 1.45 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
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using StudyEntityFramework.Context;
using StudyEntityFramework.Model;
namespace StudyEntityFramework;
internal static class Program
{
private static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
await using var db = new BloggingContext();
// Create
Console.WriteLine("Inserting a new blog...");
db.Add(new Blog { Url = "https://devblogs.microsoft.com/dotnet/" });
await db.SaveChangesAsync();
// Read
Console.WriteLine("First blog in the database:");
var blog = await db.Blogs.OrderBy(b => b.BlogId).FirstAsync();
Console.WriteLine(JsonSerializer.Serialize(blog));
// Update
Console.WriteLine("Updating the blog and adding a post...");
blog.Url = "https://devblogs.microsoft.com/dotnet/category/ef-core/";
blog.Posts.AddRange([new Post
{
Title = "Testing EF Core",
Content = "Using EF Core in .NET"
},new Post
{
Title = "Testing New Post",
Content = "With new content"
}]);
await db.SaveChangesAsync();
Console.WriteLine("Updated blog:");
Console.WriteLine(JsonSerializer.Serialize(blog));
// Delete
Console.WriteLine("Deleting the blog...");
db.Remove(blog);
await db.SaveChangesAsync();
}
}