예제 #1
0
        public static void Main()
        {
            using var db = new CarDbContext();
            db.Database.Migrate();

            //AddMakes(db);

            //AddModelsToOpelMake(db);

            //AddCarsToInsigniaModel(db);

            //AddCustomerAndPurchase(db);

            //AddAddressToCustomer(db);

            //QueringCarsOwnerByResultModels(db);

            //ValidateEntity(db);

            //SetValueToSecretPropertyInCar(db);

            var price = 5000;

            db.Cars
            .Where(c => c.Price > price)
            .ToList();

            db.Cars
            .FromSqlRaw("SELECT * FROM  Cars WHERE Price> {0}", price)
            .ToList();

            db.Cars
            .FromSqlInterpolated($"SELECT * FROM  Cars WHERE Price> {price}")
            .ToList();

            db.Cars
            .Where(c => c.Price > price)
            .Select(c => new ResultModel
            {
                FullName = c.Model.Make.Name
            })
            .ToList();

            //var query = EF.CompileQuery<CarDbContext, IEnumerable<ResultModel>>(
            //    db => db.Cars
            //   .Where(c => c.Price > price)
            //   .Select(c => new ResultModel
            //   {
            //       FullName = c.Model.Make.Name
            //   }));

            //var result = query(db);

            CarQueries.ToResult(db, price);

            using var data = new CarDbContext();

            var car = db.Cars.FirstOrDefault();

            data.Attach(car);
            car.Price += 100;

            data.Entry(car).State = EntityState.Detached;
            data.SaveChanges();

            var newCar = new Car {
                Id = 2
            };

            data.Attach(newCar); //with this block we don't make query to searche the object in the database but with Attach and Savachanges we can set the value of the object with this primary key
            newCar.Price = 15000;
            data.SaveChanges();

            //var entry = data.Entry(newCar);
            //entry.State = EntityState.Added;
            //data.SaveChanges();// EXCEPTION  we cannot set explicit value of an existing object

            //db.SaveChanges();
        }