コード例 #1
0
        public bool registerMechanic(MechanicViewModel model)
        {
            try
            {
                var             config   = new MapperConfiguration(cfg => cfg.CreateMap <MechanicViewModel, Models.Mechanic>());
                var             mapper   = new Mapper(config);
                Models.Mechanic mechanic = mapper.Map <Models.Mechanic>(model);
                mechanic.ID          = 0;
                mechanic.ExternalID  = Guid.NewGuid();
                mechanic.CreatedDate = DateTime.Now;
                mechanic.CreatedBy   = 1;
                mechanic.IsActive    = true;

                _dbContext.Mechanics.Add(mechanic);
                _dbContext.SaveChanges();
            }
            catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException ex)
            {
                Console.WriteLine(ex.InnerException);
            }
            catch (System.Data.Entity.Core.EntityCommandCompilationException ex)
            {
                Console.WriteLine(ex.InnerException);
            }
            catch (System.Data.Entity.Core.UpdateException ex)
            {
                Console.WriteLine(ex.InnerException);
            }

            catch (System.Data.Entity.Infrastructure.DbUpdateException ex) //DbContext
            {
                Console.WriteLine(ex.InnerException);
            }
            catch (Exception ex)
            {
                return(false);
            }
            return(true);
        }
コード例 #2
0
        public MechanicViewModel getMechanicByExID(string exID)
        {
            MechanicViewModel mechanic = new MechanicViewModel();

            try
            {
                Models.Mechanic Mmechanic = _dbContext.Mechanics.Where(s => exID.Equals(s.ExternalID.ToString())).FirstOrDefault();

                if (Mmechanic.Name != null)
                {
                    //// TODO: automapper mapping

                    var config = new MapperConfiguration(cfg => cfg.CreateMap <Models.Mechanic, MechanicViewModel>());
                    var mapper = new Mapper(config);
                    mechanic = mapper.Map <MechanicViewModel>(Mmechanic);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(mechanic);
        }