public void SetupAndInsertObject()
        {
            AutoFacContainer container = new AutoFacContainer();

            container.RegisterOptions <PostgreSQLEFOptions>();

            container.ContainerBuilder.Register(context =>
            {
                var options = context.Resolve <PostgreSQLEFOptions>();
                var builder = new DbContextOptionsBuilder <CodeFirstContext>();
                builder.UseNpgsql(options.ConnectionString);
                return(builder.Options);
            });

            container.ContainerBuilder.RegisterType <CodeFirstContext>();

            var serivcesContainer = container.ContainerBuilder.Build();

            using (CodeFirstContext context = serivcesContainer.Resolve <CodeFirstContext>())
            {
                context.Database.EnsureCreated();
                context.Users.Add(new User()
                {
                    Name      = "Tom",
                    YearBirth = 2001,
                    IsMale    = true
                });
                context.SaveChanges();
            }
        }
        public void QueryObject()
        {
            AutoFacContainer container = new AutoFacContainer();

            container.RegisterOptions <PostgreSQLEFOptions>();

            container.ContainerBuilder.Register(context =>
            {
                var options = context.Resolve <PostgreSQLEFOptions>();
                var builder = new DbContextOptionsBuilder <CodeFirstContext>();
                builder.UseNpgsql(options.ConnectionString);
                return(builder.Options);
            });

            container.ContainerBuilder.RegisterType <CodeFirstContext>();

            var serivcesContainer = container.ContainerBuilder.Build();

            using (CodeFirstContext context = serivcesContainer.Resolve <CodeFirstContext>())
            {
                context.Database.EnsureCreated();
                // select * from "Users" where IsMale = true and Age > 80
                var users = context.Users
                            //.Where(u => u.IsMale == true && u.YearBirth > 2005)
                            //.GroupBy(u => u.YearBirth)
                            .ToList();

                var cars = context.Cars.ToList();

                var avgCarYear = users//.AsParallel()
                                 .Select(user => cars.Where(car => car.Year == user.YearBirth && car.Id % 2 == 1))
                                 .Aggregate(new List <Car>(), (seed, _cars) =>
                {
                    seed.AddRange(_cars);
                    return(seed);
                })
                                 .Average(car => car.Year);

                Debugger.Break();
                context.SaveChanges();
            }
        }
        public void InsertRandomUsers()
        {
            AutoFacContainer container = new AutoFacContainer();

            container.RegisterOptions <PostgreSQLEFOptions>();

            container.ContainerBuilder.Register(context =>
            {
                var options = context.Resolve <PostgreSQLEFOptions>();
                var builder = new DbContextOptionsBuilder <CodeFirstContext>();
                builder.UseNpgsql(options.ConnectionString);
                return(builder.Options);
            });

            container.ContainerBuilder.RegisterType <CodeFirstContext>();

            var serivcesContainer = container.ContainerBuilder.Build();

            using (CodeFirstContext context = serivcesContainer.Resolve <CodeFirstContext>())
            {
                context.Database.EnsureCreated();
                Random rnd = new Random(DateTime.Now.Millisecond);
                for (int i = 0; i < 100; i++)
                {
                    context.Users.Add(new User()
                    {
                        Name      = $"User {i}",
                        YearBirth = (int)(1985 + rnd.NextDouble() * 30),
                        IsMale    = rnd.NextDouble() > 0.5d
                    });

                    context.Cars.Add(new Car()
                    {
                        Year = (int)(1985 + rnd.NextDouble() * 30),
                        Made = rnd.NextDouble() > 5d ? "Toyota":"BMW"
                    });
                }
                context.SaveChanges();
            }
        }