-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
95 lines (81 loc) · 2.42 KB
/
Program.cs
File metadata and controls
95 lines (81 loc) · 2.42 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
95
// See https://aka.ms/new-console-template for more information
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
var logger = factory.CreateLogger("Program");
Console.WriteLine("Hello, World!");
await using var context = new JobContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.Jobs.Add(new() { Id = Guid.NewGuid(), Name = "Job with No Error" });
context.Jobs.Add(new()
{
Id = Guid.NewGuid(),
Name = "Job with Error + Inner Error",
Error = new()
{
Code = "500",
Message = "Internal Server Error",
InnerError = new()
{
Code = "501",
Message = "Not Implemented"
}
}
});
context.Jobs.Add(new()
{
Id = Guid.NewGuid(),
Name = "Job with Error only",
Error = new()
{
Code = "400",
Message = "Bad Request"
}
});
await context.SaveChangesAsync();
context.ChangeTracker.Clear();
var jobs = await context.Jobs.ToListAsync();
foreach (var job in jobs)
{
logger.LogInformation("Processing Job: {Job}", job);
try
{
var original = context.Entry(job).OriginalValues.ToObject() as Job;
logger.LogInformation("Retrieved Original Values for Job: {Job}", job);
}
catch (Exception ex)
{
logger.LogError(ex, "Error retrieving original values for Job: {Job}", job);
}
}
Console.WriteLine("Completed");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
public class JobContext : DbContext
{
public DbSet<Job> Jobs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer("Data Source=.;Initial Catalog=ComplextTest;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=true;")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<Job>().ComplexProperty(x => x.Error, x => x.ToJson());
}
public class Job
{
public Guid Id { get; set; }
public required String Name { get; set; }
public Error? Error { get; set; }
public override string ToString()
{
return $"Job(Id={Id}, Name={Name})";
}
}
public class Error
{
public required string Code { get; set; }
public required string Message { get; set; }
public Error? InnerError { get; set; }
}