コード例 #1
0
 public void SeedData(int numBlogs, int numPostsPerBlog)
 {
     using var context = new BloggingContext();
     context.AddRange(
         Enumerable.Range(0, numBlogs).Select(_ => new Blog
     {
         Posts = Enumerable.Range(0, numPostsPerBlog).Select(_ => new Post()).ToList()
     }));
     context.SaveChanges();
 }
コード例 #2
0
 private static void SetupDatabase()
 {
     using (var db = new BloggingContext())
     {
         db.Database.EnsureDeleted();
         db.Database.EnsureCreated();
         var blogs = new List <Blog>
         {
             new Blog {
                 Name = "Skimedic's Blog", Url = "http://skimedic.com", IsDeleted = false
             },
             new Blog {
                 Name = "Lee's Blog", Url = "http://leebrandt.me", IsDeleted = true
             }
         };
         db.AddRange(blogs);
         db.SaveChanges();
     }
 }
コード例 #3
0
        public async Task GlobalSetup()
        {
            _env = new DockerEnvironmentBuilder().SetName("efcore5_tests").AddPostgresContainer("postgres").Build();
            await _env.Up();

            var container = _env.GetContainer <PostgresContainer>("postgres");

            _connectionString = container.GetConnectionString();

            using (var context = new BloggingContext(_connectionString))
            {
                context.Database.EnsureCreated();

                context.AddRange(Enumerable.Range(0, RowsCount).Select(i => new Blog
                {
                    Url         = $"1{i}3456{i}8",
                    Name        = $"Bobby-{i}-Johns",
                    CreatedDate = DateTime.UtcNow.AddDays(new Random().Next(-10, 10)),
                    UpdatedDate = DateTime.UtcNow
                }).ToList());

                context.SaveChanges();
            }
        }