|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Bogus; |
| 5 | +using Bogus.Extensions; |
| 6 | +using Microsoft.EntityFrameworkCore; |
| 7 | +using static System.Console; |
| 8 | + |
| 9 | +namespace EFCoreSeedDb |
| 10 | +{ |
| 11 | + class Program |
| 12 | + { |
| 13 | + static void Main(string[] args) |
| 14 | + { |
| 15 | + WriteLine("Bogus Blog Example!"); |
| 16 | + |
| 17 | + using var ctx = new BloggingContext(); |
| 18 | + |
| 19 | + var blogs = ctx.Blogs |
| 20 | + .Include(b => b.Posts) |
| 21 | + .ToList(); |
| 22 | + |
| 23 | + foreach( var blog in blogs) |
| 24 | + { |
| 25 | + WriteLine($"Blog Id: {blog.BlogId}"); |
| 26 | + WriteLine($"Blog Url: {blog.Url}"); |
| 27 | + foreach( var post in blog.Posts ) |
| 28 | + { |
| 29 | + WriteLine($" Post Id: {post.PostId}"); |
| 30 | + WriteLine($" Title: {post.Title}"); |
| 31 | + WriteLine($" Content: {post.Content}"); |
| 32 | + } |
| 33 | + |
| 34 | + WriteLine(); |
| 35 | + WriteLine(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + public class BloggingContext : DbContext |
| 42 | + { |
| 43 | + public DbSet<Blog> Blogs { get; set; } |
| 44 | + public DbSet<Post> Posts { get; set; } |
| 45 | + |
| 46 | + protected override void OnConfiguring(DbContextOptionsBuilder options) |
| 47 | + { |
| 48 | + options.UseSqlite("Data Source=blogging.db"); |
| 49 | + options.EnableSensitiveDataLogging(); |
| 50 | + } |
| 51 | + |
| 52 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 53 | + { |
| 54 | + base.OnModelCreating(modelBuilder); |
| 55 | + |
| 56 | + FakeData.Init(10); |
| 57 | + |
| 58 | + modelBuilder.Entity<Blog>().HasData(FakeData.Blogs); |
| 59 | + modelBuilder.Entity<Post>().HasData(FakeData.Posts); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Example uses Faker[T] |
| 66 | + /// </summary> |
| 67 | + public static class FakeData |
| 68 | + { |
| 69 | + public static Faker<Blog> BlogFaker; |
| 70 | + public static Faker<Post> PostFaker; |
| 71 | + |
| 72 | + public static List<Blog> Blogs = new List<Blog>(); |
| 73 | + public static List<Post> Posts = new List<Post>(); |
| 74 | + |
| 75 | + public static void Init(int count) |
| 76 | + { |
| 77 | + var postId = 1; |
| 78 | + PostFaker = new Faker<Post>() |
| 79 | + .RuleFor(p => p.PostId, _ => postId++) |
| 80 | + .RuleFor(p => p.Title, f => f.Hacker.Phrase()) |
| 81 | + .RuleFor(p => p.Content, f => f.Lorem.Sentence()); |
| 82 | + |
| 83 | + var blogId = 1; |
| 84 | + BlogFaker = new Faker<Blog>() |
| 85 | + .RuleFor(b => b.BlogId, _ => blogId++) |
| 86 | + .RuleFor(b => b.Url, f => f.Internet.Url()) |
| 87 | + .RuleFor(b => b.Posts, (f, b) => |
| 88 | + { |
| 89 | + var posts = PostFaker.GenerateBetween(3, 5); |
| 90 | + FakeData.Posts.AddRange(posts); |
| 91 | + |
| 92 | + foreach( var post in posts ) |
| 93 | + { |
| 94 | + post.BlogId = b.BlogId; |
| 95 | + } |
| 96 | + |
| 97 | + return null; // Blog.Posts is a getter only. The return value has no impact. |
| 98 | + }); |
| 99 | + |
| 100 | + Blogs = BlogFaker.Generate(count); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Example uses Faker facade |
| 106 | + /// </summary> |
| 107 | + public static class FakeData2 |
| 108 | + { |
| 109 | + public static int BlogId = 1; |
| 110 | + public static List<Blog> Blogs = new List<Blog>(); |
| 111 | + |
| 112 | + public static int PostId = 1; |
| 113 | + public static List<Post> Posts = new List<Post>(); |
| 114 | + |
| 115 | + private static Faker f; |
| 116 | + |
| 117 | + public static void Init(int count) |
| 118 | + { |
| 119 | + f = new Faker(); |
| 120 | + |
| 121 | + GenerateBlogs(count); |
| 122 | + } |
| 123 | + |
| 124 | + private static void GenerateBlogs(int blogCount) |
| 125 | + { |
| 126 | + for( var i = 0; i < blogCount; i++, BlogId++ ) |
| 127 | + { |
| 128 | + var blog = new Blog |
| 129 | + { |
| 130 | + BlogId = BlogId, |
| 131 | + Url = f.Internet.Url() |
| 132 | + }; |
| 133 | + |
| 134 | + Blogs.Add(blog); |
| 135 | + |
| 136 | + var postCount = f.Random.Number(3, 5); |
| 137 | + GeneratePost(blog, postCount); |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + private static void GeneratePost(Blog b, int postCount) |
| 142 | + { |
| 143 | + for( var i = 0; i < postCount; i++, PostId++ ) |
| 144 | + { |
| 145 | + var post = new Post |
| 146 | + { |
| 147 | + PostId = PostId, |
| 148 | + BlogId = b.BlogId, |
| 149 | + Title = f.Hacker.Phrase(), |
| 150 | + Content = f.Lorem.Sentence() |
| 151 | + }; |
| 152 | + |
| 153 | + Posts.Add(post); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public class Blog |
| 159 | + { |
| 160 | + public int BlogId { get; set; } |
| 161 | + public string Url { get; set; } |
| 162 | + |
| 163 | + public List<Post> Posts { get; } = new List<Post>(); |
| 164 | + } |
| 165 | + |
| 166 | + public class Post |
| 167 | + { |
| 168 | + public int PostId { get; set; } |
| 169 | + public string Title { get; set; } |
| 170 | + public string Content { get; set; } |
| 171 | + |
| 172 | + public int BlogId { get; set; } |
| 173 | + public Blog Blog { get; set; } |
| 174 | + } |
| 175 | +} |
0 commit comments