-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
109 lines (94 loc) · 3.96 KB
/
Program.cs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace ef_ids_atop_seeded_ids
{
class Program
{
static async Task Main(string[] args)
{
// Demonstrate letting the change tracker assign IDs from the get-go
Console.WriteLine("Change tracker assigns IDs from the get-go:");
using (var appDbContext = new AppDbContext())
{
await appDbContext.Database.EnsureDeletedAsync();
await appDbContext.Database.EnsureCreatedAsync();
var user1 = new User { Name = "First User" };
var user2 = new User { Name = "Second User" };
await appDbContext.Users.AddAsync(user1);
await appDbContext.Users.AddAsync(user2);
await appDbContext.Documents.AddAsync(new Document { User = user1, Content = "First user's document" });
await appDbContext.Documents.AddAsync(new Document { User = user2, Content = "Second user's document" });
await appDbContext.SaveChangesAsync();
}
using (var appDbContext = new AppDbContext())
{
foreach (var user in appDbContext.Users)
{
Console.WriteLine($"User ( Id = {user.Id}, Name = {user.Name} )");
}
foreach (var document in appDbContext.Documents)
{
Console.WriteLine($"Document ( Id = {document.Id}, UserId = {document.UserId}, Content = {document.Content} )");
}
}
// Demonstrate seeding the database with hardcoded IDs and then letting the change tracker add more
Console.WriteLine("Database is seeding with hardcoded IDs and then change tracker assigns IDs");
using (var appDbContext = new AppDbContext())
{
await appDbContext.Database.EnsureDeletedAsync();
await appDbContext.Database.EnsureCreatedAsync();
await appDbContext.Users.AddAsync(new User { Id = 1, Name = "First User" });
await appDbContext.Users.AddAsync(new User { Id = 2, Name = "Second User" });
await appDbContext.Documents.AddAsync(new Document { Id = 1, UserId = 1, Content = "First user's document" });
await appDbContext.Documents.AddAsync(new Document { Id = 2, UserId = 2, Content = "Second user's document" });
// Let the change tracker figure out an ID for this one:
await appDbContext.Users.AddAsync(new User { Name = "Third user" });
await appDbContext.SaveChangesAsync();
}
using (var appDbContext = new AppDbContext())
{
foreach (var user in appDbContext.Users)
{
Console.WriteLine($"User ( Id = {user.Id}, Name = {user.Name} )");
}
foreach (var document in appDbContext.Documents)
{
Console.WriteLine($"Document ( Id = {document.Id}, UserId = {document.UserId}, Content = {document.Content} )");
}
}
}
}
class AppDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Document> Documents { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Data Source={nameof(ef_ids_atop_seeded_ids)}.db");
optionsBuilder.EnableSensitiveDataLogging();
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(builder =>
builder
.AddConsole()
.AddFilter(DbLoggerCategory.Database.Command.Name, LogLevel.Trace)
);
var loggerFactory = serviceCollection.BuildServiceProvider().GetService<ILoggerFactory>();
optionsBuilder.UseLoggerFactory(loggerFactory);
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Document
{
public int Id { get; set; }
public int UserId { get; set; }
public User User { get; set; }
public string Content { get; set; }
}
}