Skip to content

Commit 4bd1716

Browse files
authored
Merge pull request #13 from ewdlop/ewdlop-patch-11
Refactor BloggingContext to ensure DbSet properties are non-nullable …
2 parents 1fa6d43 + d83b2bb commit 4bd1716

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

亂七八糟/亂七八糟.DataAccessLayer/BloggingContext.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public class BloggingContext : DbContext
1010
//public static readonly BloggingContext Default = new BloggingContext();
1111
//public required DbSet<Blog> Blogs { get; set; }
1212
//public required DbSet<Post>? Posts { get; set; }
13-
public DbSet<Blog>? Blogs { get; set; }
14-
public DbSet<Post>? Posts { get; set; }
13+
public DbSet<Blog> Blogs { get; set; }
14+
public DbSet<Post> Posts { get; set; }
1515
public string? DbPath { get; } = string.Empty;
1616

1717
public BloggingContext()
@@ -42,6 +42,19 @@ public BloggingContext(BloggingContext context, string dbPath)
4242
DbPath = dbPath;
4343
}
4444

45+
public BloggingContext(string dbPath)
46+
{
47+
Initialize();
48+
DbPath = dbPath;
49+
}
50+
51+
52+
protected virtual void Initialize()
53+
{
54+
Blogs = Set<Blog>();
55+
Posts = Set<Post>();
56+
}
57+
4558
//public BloggingContext(string dbPath)
4659
//{
4760
// DbPath = dbPath;

亂七八糟/亂七八糟.DataAccessLayer/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static async Task Main(string[] args)
2525
using (var scope = host.Services.CreateScope())
2626
{
2727
await using var db = new BloggingContext();
28+
2829
db.Database.EnsureCreated();
2930

3031
// Note: This sample requires the database to be created before running.

0 commit comments

Comments
 (0)