private VehicleContract CreateVehicleContract(Vehicle vehicle)
        {
            VehicleContract vehicleContract = new VehicleContract();

            vehicleContract.VehicleID = vehicle.VehicleID;

            Contract contract = db.Contracts.Where(c => c.ContractorID == vehicle.ContractorID).ToList().First();

            vehicleContract.ContractID = contract.ContractID;

            return(vehicleContract);
        }
        public async Task <VehicleContract> GetVehicleContract(int vehicleID, int contractID)
        {
            Stopwatch timespan = Stopwatch.StartNew();

            try
            {
                VehicleContract vehicleContract = db.VehiclesContracts.Where(c => c.VehicleID == vehicleID && c.ContractID == contractID).ToList().First();

                timespan.Stop();
                log.TraceApi("SQL Database", "VehicleRepository.GetVehicleContract", timespan.Elapsed, "vehicleID={0}, contractID={1}", vehicleID, contractID);

                return(await Task.Run(() => { return vehicleContract; }));
            }
            catch (Exception e)
            {
                log.Error(e, "Error in VehicleRepository.GetVehicleContract(vehicleID={0}, contractID={1})", vehicleID, contractID);
                throw;
            }
        }
        public async Task DeleteContract(int vehicleID, int contractID)
        {
            Stopwatch timespan = Stopwatch.StartNew();

            try
            {
                VehicleContract vehicleContract = db.VehiclesContracts.Where(c => c.VehicleID == vehicleID && c.ContractID == contractID).ToList().First();

                db.VehiclesContracts.Remove(vehicleContract);
                await db.SaveChangesAsync();

                timespan.Stop();
                log.TraceApi("SQL Database", "VehicleRepository.DeleteContract", timespan.Elapsed, "vehicleID={0}, contractID={1}", vehicleID, contractID);
            }
            catch (Exception e)
            {
                log.Error(e, "Error in VehicleRepository.DeleteContract(vehicleID={0}, contractID={1})", vehicleID, contractID);
                throw;
            }
        }
        public async Task DeleteAsync(int vehicleID)
        {
            Vehicle         vehicle         = null;
            VehicleContract vehicleContract = null;

            Stopwatch timespan = Stopwatch.StartNew();

            SiccoAppConfiguration.SuspendExecutionStrategy = true;

            DbContextTransaction tran = db.Database.BeginTransaction();

            try
            {
                vehicle = await db.Vehicles.FindAsync(vehicleID);

                db.Vehicles.Remove(vehicle);

                vehicleContract = await db.VehiclesContracts.FindAsync(GetVehicleContractID(vehicleID));

                if (vehicleContract != null)
                {
                    db.VehiclesContracts.Remove(vehicleContract);
                }

                db.SaveChanges();

                tran.Commit();

                timespan.Stop();
                log.TraceApi("SQL Database", "VehicleRepository.DeleteAsync", timespan.Elapsed, "vehicleID={0}", vehicleID);
            }
            catch (Exception e)
            {
                tran.Rollback();
                log.Error(e, "Error in VehicleRepository.DeleteAsync(vehicleID={0})", vehicleID);
                throw;
            }

            SiccoAppConfiguration.SuspendExecutionStrategy = false;
        }
        public async Task CreateContractAsync(VehicleContract vehicleContract)
        {
            Stopwatch timespan = Stopwatch.StartNew();

            try
            {
                db.VehiclesContracts.Add(vehicleContract);

                await db.SaveChangesAsync();

                var contract = db.Contracts.Where(c => c.ContractID == vehicleContract.ContractID).SingleOrDefault <Contract>();

                await GenerateContractorVehicleRequirements(contract.ContractorID, vehicleContract.ContractID, vehicleContract.VehicleID);


                timespan.Stop();
                log.TraceApi("SQL Database", "VehicleRepository.CreateContractAsync", timespan.Elapsed, "vehicleContract={0}", vehicleContract);
            }
            catch (Exception e)
            {
                log.Error(e, "Error in VehicleRepository.CreateContractAsync(vehicleContract={0})", vehicleContract);
                throw;
            }
        }
        private int?GetVehicleContractID(int vehicleID)
        {
            VehicleContract vehicleContract = db.VehiclesContracts.Where(c => c.VehicleID == vehicleID).ToList().FirstOrDefault();

            return(vehicleContract?.VehicleContractID);
        }