-
-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathProgram.cs
172 lines (138 loc) · 4.27 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Linq;
using Bogus;
using Bogus.Extensions;
using Microsoft.EntityFrameworkCore;
using static System.Console;
namespace EFCoreSeedDb
{
class Program
{
static void Main(string[] args)
{
WriteLine("Bogus Blog Example!");
using var ctx = new BloggingContext();
var blogs = ctx.Blogs
.Include(b => b.Posts)
.ToList();
foreach( var blog in blogs)
{
WriteLine($"Blog Id: {blog.BlogId}");
WriteLine($"Blog Url: {blog.Url}");
foreach( var post in blog.Posts )
{
WriteLine($" Post Id: {post.PostId}");
WriteLine($" Title: {post.Title}");
WriteLine($" Content: {post.Content}");
}
WriteLine();
WriteLine();
}
}
}
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlite("Data Source=blogging.db");
options.EnableSensitiveDataLogging();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
FakeData.Init(10);
modelBuilder.Entity<Blog>().HasData(FakeData.Blogs);
modelBuilder.Entity<Post>().HasData(FakeData.Posts);
}
}
/// <summary>
/// Example uses Faker[T]
/// </summary>
public static class FakeData
{
public static List<Blog> Blogs = new List<Blog>();
public static List<Post> Posts = new List<Post>();
public static void Init(int count)
{
var postId = 1;
var postFaker = new Faker<Post>()
.RuleFor(p => p.PostId, _ => postId++)
.RuleFor(p => p.Title, f => f.Hacker.Phrase())
.RuleFor(p => p.Content, f => f.Lorem.Sentence());
var blogId = 1;
var blogFaker = new Faker<Blog>()
.RuleFor(b => b.BlogId, _ => blogId++)
.RuleFor(b => b.Url, f => f.Internet.Url())
.RuleFor(b => b.Posts, (f, b) =>
{
postFaker.RuleFor(p => p.BlogId, _ => b.BlogId);
var posts = postFaker.GenerateBetween(3, 5);
FakeData.Posts.AddRange(posts);
return null; // Blog.Posts is a getter only. The return value has no impact.
});
var blogs = blogFaker.Generate(count);
FakeData.Blogs.AddRange(blogs);
}
}
/// <summary>
/// Example uses Faker facade
/// </summary>
public static class FakeData2
{
public static int BlogId = 1;
public static List<Blog> Blogs = new List<Blog>();
public static int PostId = 1;
public static List<Post> Posts = new List<Post>();
private static Faker f;
public static void Init(int count)
{
f = new Faker();
GenerateBlogs(count);
}
private static void GenerateBlogs(int blogCount)
{
for( var i = 0; i < blogCount; i++, BlogId++ )
{
var blog = new Blog
{
BlogId = BlogId,
Url = f.Internet.Url()
};
Blogs.Add(blog);
var postCount = f.Random.Number(3, 5);
GeneratePost(blog, postCount);
};
}
private static void GeneratePost(Blog b, int postCount)
{
for( var i = 0; i < postCount; i++, PostId++ )
{
var post = new Post
{
PostId = PostId,
BlogId = b.BlogId,
Title = f.Hacker.Phrase(),
Content = f.Lorem.Sentence()
};
Posts.Add(post);
}
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; } = new List<Post>();
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
}