Esempio n. 1
0
 public virtual void Attach(TEntity entityToAttach)
 {
     if (context.Entry(entityToAttach).State == EntityState.Detached)
     {
         dbSet.Attach(entityToAttach);
     }
 }
Esempio n. 2
0
        public void Delete(TEntity item)
        {
            if (_context.Entry(item).State == EntityState.Detached)
            {
                _dbSet.Attach(item);
            }

            _dbSet.Remove(item);
        }
        public IActionResult EditCarFinal(EditCarVM car)
        {
            List <Car> cars   = db.Cars.ToList();
            Car        newCar = new Car()
            {
                CarId = car.CarId, Type = car.Type, Price = car.Price, Name = car.Name, MSRP = car.MSRP, ImgPath = car.ImgPath
            };

            foreach (Car c in cars)
            {
                if (c.CarId == car.CarId)
                {
                    db.Entry(c).CurrentValues.SetValues(newCar);
                }
            }
            db.SaveChanges();

            List <Car> cars2 = db.Cars.ToList();
            CarListVM  model = new CarListVM()
            {
                cars = cars2
            };

            return(View("Index", model));
        }
Esempio n. 4
0
        public IHttpActionResult PutCar([FromUri] int id, [FromBody] Car car)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != car.CarId)
            {
                return(BadRequest());
            }

            _db.Entry(car).State = EntityState.Modified;

            try
            {
                _db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CarExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Esempio n. 5
0
        public async Task <IActionResult> PutEmployee([FromRoute] int id, [FromBody] Car car)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != car.CarId)
            {
                return(BadRequest());
            }

            _context.Entry(car).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CarExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Esempio n. 6
0
        public async Task <IActionResult> PutBooking(int id, Booking booking)
        {
            if (id != booking.BookingId)
            {
                return(BadRequest());
            }

            _context.Entry(booking).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookingExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Esempio n. 7
0
        public IActionResult EditOrderFinal(EditOrderVM order)
        {
            List <Order> orders   = db.Orders.ToList();
            Order        newOrder = new Order()
            {
                OrderId = order.OrderId, CarName = order.CarName, ClientName = order.ClientName, Telephone = order.Telephone
            };

            foreach (Order o in orders)
            {
                if (o.OrderId == order.OrderId)
                {
                    db.Entry(o).CurrentValues.SetValues(newOrder);
                }
            }
            db.SaveChanges();

            List <Order> orders2 = db.Orders.ToList();
            OrderListVM  model   = new OrderListVM()
            {
                Orders = orders2
            };

            return(View("Index", model));
        }
Esempio n. 8
0
        public async Task <IActionResult> PutCar(int id, Car car)
        {
            if (id != car.Id)
            {
                return(BadRequest());
            }

            _context.Entry(car).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CarExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Esempio n. 9
0
 public ActionResult Edit([Bind(Include = "ID,Name,Mileage,Color")] Car car)
 {
     if (ModelState.IsValid)
     {
         db.Entry(car).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(car));
 }
Esempio n. 10
0
        private static void ExplicitLoading(CarDbContext db)
        {
            var car = db.Cars.FirstOrDefault(c => c.Id == 1);

            db.Entry(car)
            .Reference(c => c.Model)
            .Load();

            Console.WriteLine(car.Model.Name);
        }
        public async Task <ActionResult <Cars> > PutCar(int id, Cars car)
        {
            if (id != car.Id)
            {
                return(BadRequest());
            }
            _context.Entry(car).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Esempio n. 12
0
        [HttpPut("{Id}")] // /api/company/1--(id)-- PUT: UPDATING Vehicle
        public async Task <ActionResult <Vehicles> > PutCar(int Id, Vehicles vehicle)
        {
            if (Id != vehicle.Id)
            {
                return(BadRequest());
            }

            _context.Entry(vehicle).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Esempio n. 13
0
        public async Task <ActionResult> PutCar(int id, Car updatingCar)
        {
            if (id != updatingCar.Id || !ModelState.IsValid)
            {
                return(BadRequest());
            }
            else
            {
                _context.Entry(updatingCar).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(NoContent());
            }
        }
Esempio n. 14
0
 public void Add(Car car)
 {
     if (car.CarDailyPrice > 0 && car.CarDescription.Length >= 2)
     {
         using (CarDbContext context = new CarDbContext())
         {
             var addedCar = context.Entry(car);
             addedCar.State = EntityState.Added;
             context.SaveChanges();
         }
     }
     else
     {
         Console.WriteLine("Kriterlere uygun şekilde araba girişi yapmadınız!");
     }
 }
Esempio n. 15
0
 public int Deleted(T entity)
 {
     _db.Entry(entity).State = EntityState.Deleted;
     return(SaveChanges());
 }
Esempio n. 16
0
 public bool UpdateCars(Cars cars)
 {
     _carDbContext.cars.Attach(cars);
     _carDbContext.Entry(cars).State = EntityState.Modified;
     return(true);
 }
 public void UpdateCar(int IdCar, Car updatedCar)
 {
     _context.Cars.Attach(updatedCar);
     _context.Entry(updatedCar).State = EntityState.Modified;
     _context.SaveChanges();
 }
Esempio n. 18
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();
        }
        public static void Main(string[] args)
        {
            using var db = new CarDbContext();

            db.Database.Migrate();

            using var data = new CarDbContext();

            var car = data.Cars
                      .FirstOrDefault(x => x.Id == 1);

            data.Entry(car)
            .Collection(x => x.Owners)
            .Load();

            Console.WriteLine(car.Owners.FirstOrDefault().PurchaseDate);

            ;

            //var cars = data.Cars
            //    .Where(x => x.Price < 20000)
            //    .Update(x => new Car { Price = x.Price * 1.2m });

            //data.Cars
            //    .Where(x => x.Price < 200000)
            //    .ToList()
            //    .ForEach(x => x.Price *= 1.2m);

            //data.SaveChanges();



            //var price = 5000;
            //
            //var stopwatch = Stopwatch.StartNew();
            //
            //db.Cars
            //    .Where(x => x.Price > price)
            //    .Where(x => x.Owners.Any(x => x.Customer.LastName == null))
            //    .Select(x => new ResultModel
            //    {
            //        Count = x.Owners.Count(x => x.Customer.Purchases.Count > 1)
            //    })
            //    .ToList();
            //
            //Console.WriteLine(stopwatch.Elapsed + " Cold");
            //
            //stopwatch = Stopwatch.StartNew();
            //
            //db.Cars
            //    .Where(x => x.Price > price)
            //    .Where(x => x.Owners.Any(x => x.Customer.LastName == null))
            //    .Select(x => new ResultModel
            //    {
            //        Count = x.Owners.Count(x => x.Customer.Purchases.Count > 1)
            //    })
            //    .ToList();
            //
            //Console.WriteLine(stopwatch.Elapsed + " Warm");
            //
            //stopwatch = Stopwatch.StartNew();
            //
            //CarQueries.ToResult(db, price);
            //
            //Console.WriteLine(stopwatch.Elapsed + " Compiled Query With Compilation");
            //
            //stopwatch = Stopwatch.StartNew();
            //
            //CarQueries.ToResult(db, price);
            //
            //Console.WriteLine(stopwatch.Elapsed + " Compiled Query");

            //var result = db.Purchases.Select(x => new PuchaseResultModel
            //{
            //    Price = x.Price,
            //    PurchaseDate = x.PurchaseDate,
            //    Customer = new CustomerResultModel
            //    {
            //        Name = x.Customer.FirstName + " " + x.Customer.LastName,
            //        Town = x.Customer.Address.Town
            //    },
            //    Car = new CarResultModel
            //    {
            //        Vin = x.Car.Vin,
            //        Make = x.Car.Model.Make.Name,
            //        Model = x.Car.Model.Name
            //    }
            //}).ToList();

            //var make = db.Makes.FirstOrDefault(x => x.Name == "Mercedes");
            //
            //var model = new Model
            //{
            //    Modification = "500",
            //    Name = "CL",
            //    Year = 3000
            //};
            //
            //make.Models.Add(model);
        }
        private static void SetValueToSecretPropertyInCar(CarDbContext db)
        {
            var car = db.Cars.FirstOrDefault();

            db.Entry(car).Property <int>("MySecretProperty").CurrentValue = 15;
        }
 void ICarService.Delete(Car.Entities.Car car)
 {
     _Context.Entry(car).State = EntityState.Deleted;
     _Context.SaveChanges();
 }