public static void EnsureSeedData(this BlogDbContext context, UserManager <IdentityUser> userManager) { var defaultUser = new IdentityUser { UserName = "******", Email = "*****@*****.**" }; if (!context.Users.AnyAsync().Result) { userManager.CreateAsync(defaultUser, "P@asW0rd").Wait(); userManager.AddClaimsAsync(defaultUser, new [] { new Claim(ClaimTypes.Name, "Default"), new Claim(ClaimTypes.Surname, "Author") }).Wait(); } if (!context.Posts.AnyAsync().Result) { var inMemoryPostStore = new InMemoryPosts(); var store = new PostsSqlServerRepository(context); var result = inMemoryPostStore.GetLatestApprovedPosts(0, 100).Result; var tags = result.Items.SelectMany(x => x.Tags); context.AddRange(tags.Select(tag => new TagEntity { Name = tag.Name, Slug = tag.Slugs.First().Path, CreatedBy = defaultUser, CreationIpAddress = "127.0.0.1", CreatedOnUtc = DateTime.UtcNow })); context.SaveChanges(); foreach (var post in result.Items) { var newPostCommand = new CreatePostCommand(post.Title, post.Abstract, post.Content, post.CreationRecord.IpAddress, new CreatePostCommand.User { Id = defaultUser.Id, Name = defaultUser.UserName }, post.Tags.Select(x => x.Name).ToList().AsReadOnly(), true); store.CreatePost(newPostCommand).Wait(); } } }
public async Task SeedPosts() { if (!_dbContext.Posts.Any()) { var posts = new List <Post> { new Post { CreationDate = new System.DateTime(2019, 01, 01), Html = "<p>Hello! I'm Mike, a .NET-oriented developer, and this is my tech blog. I'm not usually the blogging type but as a developer you accumulate so much random knowledge that often gets lost or forgotten - it seemed like it was time to start recording some of it, so I can refer back to it, and if it helps anyone else out there even better :) <br/> This is my own blog site that I made for myself in ASP.NET Core MVC and hosted on Azure. It has a few useful features, like a simple search function, and a protected Admin section where the blog site owner can create, save (and in future edit) blog posts. I hope to add new features in the future. <br/><br/> The code hosted on GitHub <a href=\"https://github.com/zola-25/Blog\">here</a>. </p>", Title = "New Blog Site", UrlSegment = "NewBlog" } }; _dbContext.AddRange(posts); await _dbContext.SaveChangesAsync(); } }