Beispiel #1
0
        public static void AddAuto(Auto newAuto)
        {
            using (CarsEntity autorentEntityContext = new CarsEntity())
            {
                try
                {
                    Models.Auto.Auto auto = new Models.Auto.Auto
                    {
                        Number = newAuto.CarNumber,
                        ModelId = newAuto.ModelID,
                        BodyType = (int)newAuto.BodyType,
                        InsuaranceId = newAuto.InsuranceNumber,
                        Year = newAuto.CreationYear,
                        Mileage = newAuto.CurrentMilage,
                        Engine = newAuto.Engine,
                        ColorGroup = newAuto.CarColor,
                        DayRate = newAuto.DayRate,
                        KmRate = newAuto.KmRate,
                        Status = (short)newAuto.Status,
                        Advance = newAuto.Advance
                    };

                    if (!ReferenceEquals(newAuto.PhotoFileName, null))
                    {
                        Models.Auto.AutoPhoto autoPhotos = new AutoPhoto
                        {
                            AutoNumber = newAuto.CarNumber,
                            DoDate = DateTime.UtcNow,
                            PhotoFileName = newAuto.PhotoFileName
                        };

                        auto.AutoPhotos.Add(autoPhotos);
                    }

                    autorentEntityContext.Autoes.AddObject(auto);
                    autorentEntityContext.SaveChanges();
                }
                catch (InvalidOperationException ex)
                {
                    throw ex;
                }
            }
        }
Beispiel #2
0
        public static void UpdateAuto(string number, string name, Guid modelID, int bodyType, string insuranceID, short year, int milage, int engine, string color, int dayRate, int kmRate, short status, decimal minRate, string photoFileName)
        {
            using (CarsEntity autorentEntityContext = new CarsEntity())
            {
                try
                {
                    Models.Auto.Auto auto = new Models.Auto.Auto
                    {
                        Number = number,
                        Name = name,
                        ModelId = modelID,
                        BodyType = bodyType,
                        InsuaranceId = insuranceID,
                        Year = year,
                        Mileage = milage,
                        Engine = engine,
                        ColorGroup = color,
                        DayRate = dayRate,
                        KmRate = kmRate,
                        Status = status,
                        Advance = minRate
                    };

                    var queryAuto = (from a in autorentEntityContext.Autoes where a.Number == number select a).FirstOrDefault();
                    var queryPhoto = (from p in autorentEntityContext.AutoPhotos where p.AutoNumber == number select p).FirstOrDefault();

                    queryAuto.AutoPhotos.Clear();

                    auto.AutoPhotos.Add(new AutoPhoto
                    {
                        AutoNumber = auto.Number,
                        DoDate = DateTime.UtcNow,
                        PhotoFileName = photoFileName
                    });

                    queryAuto = auto;

                    autorentEntityContext.SaveChanges();
                }
                catch (InvalidOperationException ex)
                {
                    throw ex;
                }
            }
        }