private void btn_Add_Click(object sender, EventArgs e)
        {
            if (Validation())
            {
                var token = new AutomobileSaveToken()
                {
                    DRN                  = txt_DRN.Text,
                    EngineID             = ((EngineViewModel)cmb_Engine.SelectedItem).Id,
                    Year                 = int.Parse(txt_Year.Text),
                    ChassiNumber         = txt_Chassi.Text,
                    ColorId              = ((ColorViewModel)cmb_Color.SelectedItem).Id,
                    OwnerName            = txt_Owner.Text,
                    OwnerTelephoneNumber = txt_Telephone.Text,
                    Description          = rtxt_Description.Text
                };

                if (Model == null)
                {
                    automobileController.SaveAutomobile(token);
                }
                else
                {
                    automobileController.UpdateAutomobile(token, Model);
                }



                this.Close();
            }
        }
        public void SaveAutomobile(AutomobileSaveToken token)
        {
            var model = MakeModelFromToken(token);
            var mc    = new MaintenanceCardDataModel();

            model.MaintenanceCards.Add(mc);
            mc.Automobile   = model;
            mc.AutomobileId = model.Id;

            context.Automobiles.Add(model);
            context.Maintenances.Add(mc);
            context.SaveChanges();
        }
        private AutomobileDataModel MakeModelFromToken(AutomobileSaveToken token)
        {
            OwnerDataModel      owner  = null;
            AutomobileDataModel result = new AutomobileDataModel();
            ColorDataModel      color  = null;
            EngineDataModel     engine = null;


            owner  = context.Owners.FirstOrDefault(o => o.TelephoneNumber == token.OwnerTelephoneNumber);
            color  = context.Colors.FirstOrDefault(c => c.Id == token.ColorId);
            engine = context.Engines.FirstOrDefault(e => e.Id == token.EngineID);


            if (owner == null)
            {
                owner = new OwnerDataModel()
                {
                    Name            = token.OwnerName,
                    TelephoneNumber = token.OwnerTelephoneNumber,
                };
            }

            result.DRN          = token.DRN;
            result.ChassiNumber = token.ChassiNumber;
            result.Color        = color;
            result.ColorId      = color.Id;
            result.Description  = token.Description;
            result.Engine       = engine;
            result.EngineId     = engine.Id;

            result.Owner   = owner;
            result.OwnerId = owner.Id;
            result.Year    = token.Year;

            if (!owner.Automobiles.Contains(result))
            {
                owner.Automobiles.Add(result);
            }

            return(result);
        }
        public void UpdateAutomobile(AutomobileSaveToken token, AutomobileDataModel model)
        {
            var updatedModel = MakeModelFromToken(token);

            updatedModel.Id = model.Id;
            var _model = context.Automobiles.FirstOrDefault(a => a.Id == model.Id);

            if (_model != null)
            {
                _model.ChassiNumber = updatedModel.ChassiNumber;
                _model.Color        = updatedModel.Color;
                _model.ColorId      = updatedModel.ColorId;
                _model.Description  = updatedModel.Description;
                _model.DRN          = updatedModel.DRN;
                _model.Engine       = updatedModel.Engine;
                _model.EngineId     = updatedModel.EngineId;

                _model.Owner   = updatedModel.Owner;
                _model.OwnerId = updatedModel.OwnerId;
                _model.Year    = updatedModel.Year;
            }
            EditEntity <AutomobileDataModel>(_model);
        }